Normally, files have suffixes, such as: xxx.gif, xxxx.avi. However, there are also files with suffixes. Removing the suffix of the file will not affect the content of the file itself. So in the data of the file, there are other ways to identify the type of the file.
In programming, the constant that identifies the file format is called Magic Number , formerly called: File Signatures (file signature).
Magic Number has the following three types of use in programming:
- A unique value whose meaning cannot be explained, or a value that is referenced multiple times in a program but can be replaced by a named constant.
- A constant value or character identifying the type of text.
- Unique values that are not easily misinterpreted as other meanings, such as globally unique identifiers.
Use Magic Number to determine file type
1. Request to obtain file data and convert it to Array Buffer
function loadFile(url) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest()
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
resolve(xhr)
}
}
xhr.onerror = reject
xhr.open('GET', url, true)
xhr.responseType = 'arraybuffer'
xhr.send('')
})
}
2. Buffer to hexadecimal string
function buf2hex(buffer) {
return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('')
}
3. Obtain the file header according to the hexadecimal string and determine the file type
function getFileType(url) {
loadFile(url).then(xhr => {
const hex = buf2hex(xhr.response)
console.log(hex)
// todo....
})
}
test:
Common file header magic numbers
JPEG (jpg),文件头:ffd8ff
PNG (png),文件头:89504E47
GIF (gif),文件头:47494638
TIFF (tif),文件头:49492A00
Windows Bitmap (bmp),文件头:424D
CAD (dwg),文件头:41433130
Adobe Photoshop (psd),文件头:38425053
Rich Text Format (rtf),文件头:7B5C727466
XML (xml),文件头:3C3F786D6C
HTML (html),文件头:68746D6C3E
Email [thorough only] (eml),文件头:44656C69766572792D646174653A
Outlook Express (dbx),文件头:CFAD12FEC5FD746F
Outlook (pst),文件头:2142444E
MS Word/Excel (xls.or.doc),文件头:D0CF11E0
MS Access (mdb),文件头:5374616E64617264204A
WordPerfect (wpd),文件头:FF575043
Adobe Acrobat (pdf),文件头:255044462D312E
Quicken (qdf),文件头:AC9EBD8F
Windows Password (pwl),文件头:E3828596
ZIP Archive (zip),文件头:504B0304
RAR Archive (rar),文件头:52617221
Wave (wav),文件头:57415645
AVI (avi),文件头:41564920
Real Audio (ram),文件头:2E7261FD
Real Media (rm),文件头:2E524D46
MPEG (mpg),文件头:000001BA
MPEG (mpg),文件头:000001B3
Quicktime (mov),文件头:6D6F6F76
Windows Media (asf),文件头:3026B2758E66CF11
MIDI (mid),文件头:4D546864
mp3: 494433
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。