Created
October 4, 2017 06:13
-
-
Save Graniron/36955b4bb10cc72afb1d49eb396756ea to your computer and use it in GitHub Desktop.
Get file type from array buffer(based on hexadecimal/magic-number value)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getImageType(arrayBuffer){ | |
var type = ""; | |
var dv = new DataView(arrayBuffer,0,5); | |
var nume1 = dv.getUint8(0,true); | |
var nume2 = dv.getUint8(1,true); | |
var hex = nume1.toString(16) + nume2.toString(16) ; | |
switch(hex){ | |
case "8950": | |
type = "image/png"; | |
break; | |
case "4749": | |
type = "image/gif"; | |
break; | |
case "424d": | |
type = "image/bmp"; | |
break; | |
case "ffd8": | |
type = "image/jpeg"; | |
break; | |
default: | |
type = null; | |
break; | |
} | |
return type; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment