Skip to content

Instantly share code, notes, and snippets.

@GiantZOC
GiantZOC / MimeTypeVerification.js
Last active April 9, 2024 11:08
Check the header to see the file type
//https://stackoverflow.com/questions/18299806/how-to-check-file-mime-type-with-javascript-before-upload/29672957#29672957
// Return the first few bytes of the file as a hex string
function getBLOBFileHeader(url, blob, callback) {
var fileReader = new FileReader();
fileReader.onloadend = function (e) {
var arr = (new Uint8Array(e.target.result)).subarray(0, 4);
var header = "";
for (var i = 0; i < arr.length; i++) {
header += pad(arr[i].toString(16), 2);