JS端判断:
真实文件代码列表:
https://www.garykessler.net/library/file_sigs.html
//https://www.garykessler.net/library/file_sigs.html
//https://stackoverflow.com/questions/18299806/how-to-check-file-mime-type-with-javascript-before-upload
/**
* Load the mime type based on the signature of the first bytes of the file
* @param {File} file A instance of File
* @param {Function} callback Callback with the result
* @author Victor www.vitim.us
* @date 2017-03-23
*/
function loadMime(file, callback) {
//List of known mimes
var mimes = [
{
mime: 'image/jpeg',
pattern: [0xFF, 0xD8, 0xFF],
mask: [0xFF, 0xFF, 0xFF],
},
{
mime: 'image/png',
pattern: [0x89, 0x50, 0x4E, 0x47],
mask: [0xFF, 0xFF, 0xFF, 0xFF],
},
{
mime: 'pdf',
pattern: [0x25, 0x50, 0x44, 0x46],
mask: [0xFF, 0xFF, 0xFF, 0xFF],
}
// you can expand this list @see https://mimesniff.spec.whatwg.org/#matching-an-image-type-pattern
];
function check(bytes, mime) {
for (var i = 0, l = mime.mask.length; i < l; ++i) {
if ((bytes[i] & mime.mask[i]) - mime.pattern[i] !== 0) {
return false;
}
}
return true;
}
var blob = file.slice(0, 4); //read the first 4 bytes of the file
var reader = new FileReader();
reader.onloadend = function (e) {
if (e.target.readyState === FileReader.DONE) {
var bytes = new Uint8Array(e.target.result);
for (var i = 0, l = mimes.length; i < l; ++i) {
//if (check(bytes, mimes[i])) return callback("Mime: " + mimes[i].mime + " <br> Browser:" + file.type);
if (check(bytes, mimes[i])) return callback(mimes[i].mime);
}
return callback("unknown");
//return callback("Mime: unknown <br> Browser:" + file.type);
}
};
reader.readAsArrayBuffer(blob);
}
function readFile(file, callback) {
var reader = new FileReader();
reader.onload = callback
reader.readAsDataURL(file);//base64 code file
//debugger;
}
上传按钮事件判断:
loadMime(e.target.files[0], function (mime) {
if (mime == 'unknown') {
//do something
return;
}
switch (mime) {
case "pdf":
readFile(e.target.files[0], function (e) {
//do something
});
break;
case "image/jpeg":
case "image/png":
readFile(e.target.files[0], function (e) {
//do something
});
break;
}
});
服务器端判断
真实文件类型列表:
-
JPG = 255216,
-
GIF = 7173,
-
BMP = 6677,
-
PNG = 13780,
-
COM = 7790,
-
EXE = 7790,
-
DLL = 7790,
-
RAR = 8297,
-
ZIP = 8075,
-
XML = 6063,
-
HTML = 6033,
-
ASPX = 239187,
-
CS = 117115,
-
JS = 119105,
-
TXT = 210187,
-
SQL = 255254,
-
BAT = 64101,
-
BTSEED = 10056,
-
RDP = 255254,
-
PSD = 5666,
-
PDF = 3780,
-
CHM = 7384,
-
LOG = 70105,
-
REG = 8269,
-
HLP = 6395,
-
DOC = 208207,
-
XLS = 208207,
-
DOCX = 208207,
-
XLSX = 208207,
Private Function myFileTrueType(ByVal file As HttpPostedFileBase) As String
Dim _Buf As Byte() = New Byte(file.InputStream.Length) {}
file.InputStream.Read(_Buf, 0, file.InputStream.Length)
Dim _Stream As IO.MemoryStream = New IO.MemoryStream(_Buf)
Dim _BinReader As IO.BinaryReader = New IO.BinaryReader(_Stream)
Dim _FileClass As String = String.Empty
Dim _Buffer As Byte = _BinReader.ReadByte
_FileClass = _Buffer.ToString
_Buffer = _BinReader.ReadByte
_FileClass += _Buffer.ToString
_BinReader.Close()
_Stream.Dispose()
''获取文件真实类型结束
''文件类型说明
''JPG = 255216
''GIF = 7173
''BMP = 6677
''PDF = 3780
''doc xls 208207
''pptx docx 8075
''mp4 00
Return _FileClass
End Function