This only read two bytes, it is not enough. Not all the types are only need 2 bytes, most of them need more than 3 bytes. Different file type use different header length to store the header information.
Here is a tool to read the Hex format of a file, download
UltraEdit to open the file and read the first line by hex format.
Here is my demo for you, good luck.
public string GetActualFileType(Stream stream)
{
try
{
using (System.IO.BinaryReader sr = new System.IO.BinaryReader(stream))
{
byte[] header = new byte[16];
sr.Read(header, 0, 16);
StringBuilder hexString = new StringBuilder(header.Length);
for (int i = 0; i < header.Length; i++)
{
hexString.Append(header[i].ToString("X2"));
}
return GetFileTypeByCode(hexString.ToString());
}
}
catch
{
return "undefined";
}
}
private string GetFileTypeByCode(string code)
{
Dictionary<string, string> fileheadlist = new Dictionary<string, string>();
fileheadlist.Add("GIF", "47494638");
fileheadlist.Add("PNG", "89504E47");
fileheadlist.Add("JPEG", "FFD8FF");
fileheadlist.Add("TIFF", "49492A00");
fileheadlist.Add("Windows Bitmap (bmp)", "424D");
fileheadlist.Add("CAD (dwg)", "41433130");
fileheadlist.Add("Adobe Photoshop (psd)", "38425053");
fileheadlist.Add("Rich Text Format (rtf)", "7B5C727466");
fileheadlist.Add("XML (xml)", "3C3F786D6C");
fileheadlist.Add("HTML (html)", "68746D6C3E");
fileheadlist.Add("Email [thorough only](eml)", "44656C69766572792D646174653A");
fileheadlist.Add("Outlook Express (dbx)", "CFAD12FEC5FD746F");
fileheadlist.Add("Outlook (pst)", "2142444E");
fileheadlist.Add("MS Word/Excel (xls.or.doc)", "D0CF11E0");
fileheadlist.Add("MS Access (mdb)", "5374616E64617264204A");
fileheadlist.Add("WordPerfect (wpd)", "FF575043");
fileheadlist.Add("Postscript. (eps.or.ps)", "252150532D41646F6265");
fileheadlist.Add("Adobe Acrobat (pdf)", "255044462D312E");
fileheadlist.Add("Quicken (qdf)", "AC9EBD8F");
fileheadlist.Add("ZIP Archive (zip)", "504B0304");
fileheadlist.Add("RAR Archive (rar)", "52617221");
fileheadlist.Add("Wave (wav)", "57415645");
fileheadlist.Add("AVI (avi)", "41564920");
fileheadlist.Add("Real Audio (ram)", "2E7261FD");
fileheadlist.Add("Real Media (rm)", "2E524D46");
fileheadlist.Add("MPEG (mpg)", "000001BA");
fileheadlist.Add("MPEG(mpg)", "000001B3");
fileheadlist.Add("Quicktime (mov)", "6D6F6F76");
fileheadlist.Add("Windows Media (asf)", "3026B2758E66CF11");
fileheadlist.Add("MIDI (mid)", "4D546864");
fileheadlist.Add("3GP", "000000146674797033677034");
fileheadlist.Add("MP4", "000000206674797069736F6D");
var vals = from p in fileheadlist
where code.StartsWith(p.Value)
select p;
KeyValuePair<string, string> result = vals.First();
foreach (var i in vals)
{
if (i.Value.Length > result.Value.Length)
result = i;
}
return result.Key ?? "other";
}
}
Even IE doen't recognize 3gp if you change extension (normally it identify ContentType even if extension is changed manually). Problem seems to be not so easy
As I know, 3gp and mp4 is a very special file type. It is just like a container, there would be different headers for the same extension. But we can find "66747970336770", the bytes from position 3 to position 9 are same, it is tell us the the file type
which we can find out from UE. Maybe we need to do some more complicated logic to identity the 3gp and mp4 files.
Marked as answer by TimFromKharkov on May 11, 2010 10:03 AM
TimFromKhark...
Member
188 Points
127 Posts
FileUpload ContentType Trickery - 2
May 05, 2010 04:14 PM|LINK
This discussion continues this one http://forums.asp.net/t/1331793.aspx
To get real type of uploaded file through FileUpload I use this code (because only IE set ContentType properly):
fileType = GetActualFileType(FileUpload1.PostedFile.InputStream);
public static string GetActualFileType(Stream stream)
{
System.IO.BinaryReader r = new System.IO.BinaryReader(stream);
StringBuilder fileclass = new StringBuilder();
byte buffer;
try
{
buffer = r.ReadByte();
fileclass.Append(buffer.ToString());
buffer = r.ReadByte();
fileclass.Append(buffer.ToString());
return GetFileTypeByCode(fileclass.ToString());
}
catch
{
return "undefined";
}
}
private static string GetFileTypeByCode(string code)
{
switch (code)
{
case "7076":
return "flv";
case "8273":
return "avi";
case "4838":
return "wmv";
case "7173":
return "gif";
case "255216":
return "jpeg";
case "13780":
return "png";
default:
return "other";
}
}
I worked just great, but now I need to work with 3gp and MP4.
Code in both cases is "00", does anyone know way to resolve this?
TimFromKhark...
Member
188 Points
127 Posts
Re: FileUpload ContentType Trickery - 2
May 07, 2010 09:48 AM|LINK
writing this just to make this post be of first page again...
Don't trust noboby was facing same problem..
craig1376
Member
196 Points
93 Posts
Re: FileUpload ContentType Trickery - 2
May 07, 2010 01:43 PM|LINK
Why not just check the file extension to determin what type of file is uploaded?
You could do the following:
FileInfo fileInfo = new FileInfo(fileuploadcontrol.FileName);
switch (fileInfo.Extension)
{
case ".flv":
break;
}
TimFromKhark...
Member
188 Points
127 Posts
Re: FileUpload ContentType Trickery - 2
May 08, 2010 05:14 PM|LINK
Because file extension can be changed.
Jerry Weng -...
All-Star
29527 Points
3488 Posts
Re: FileUpload ContentType Trickery - 2
May 10, 2010 05:58 AM|LINK
Hi,
This only read two bytes, it is not enough. Not all the types are only need 2 bytes, most of them need more than 3 bytes. Different file type use different header length to store the header information.
Here is a tool to read the Hex format of a file, download UltraEdit to open the file and read the first line by hex format.
Here is my demo for you, good luck.
public string GetActualFileType(Stream stream) { try { using (System.IO.BinaryReader sr = new System.IO.BinaryReader(stream)) { byte[] header = new byte[16]; sr.Read(header, 0, 16); StringBuilder hexString = new StringBuilder(header.Length); for (int i = 0; i < header.Length; i++) { hexString.Append(header[i].ToString("X2")); } return GetFileTypeByCode(hexString.ToString()); } } catch { return "undefined"; } } private string GetFileTypeByCode(string code) { Dictionary<string, string> fileheadlist = new Dictionary<string, string>(); fileheadlist.Add("GIF", "47494638"); fileheadlist.Add("PNG", "89504E47"); fileheadlist.Add("JPEG", "FFD8FF"); fileheadlist.Add("TIFF", "49492A00"); fileheadlist.Add("Windows Bitmap (bmp)", "424D"); fileheadlist.Add("CAD (dwg)", "41433130"); fileheadlist.Add("Adobe Photoshop (psd)", "38425053"); fileheadlist.Add("Rich Text Format (rtf)", "7B5C727466"); fileheadlist.Add("XML (xml)", "3C3F786D6C"); fileheadlist.Add("HTML (html)", "68746D6C3E"); fileheadlist.Add("Email [thorough only](eml)", "44656C69766572792D646174653A"); fileheadlist.Add("Outlook Express (dbx)", "CFAD12FEC5FD746F"); fileheadlist.Add("Outlook (pst)", "2142444E"); fileheadlist.Add("MS Word/Excel (xls.or.doc)", "D0CF11E0"); fileheadlist.Add("MS Access (mdb)", "5374616E64617264204A"); fileheadlist.Add("WordPerfect (wpd)", "FF575043"); fileheadlist.Add("Postscript. (eps.or.ps)", "252150532D41646F6265"); fileheadlist.Add("Adobe Acrobat (pdf)", "255044462D312E"); fileheadlist.Add("Quicken (qdf)", "AC9EBD8F"); fileheadlist.Add("ZIP Archive (zip)", "504B0304"); fileheadlist.Add("RAR Archive (rar)", "52617221"); fileheadlist.Add("Wave (wav)", "57415645"); fileheadlist.Add("AVI (avi)", "41564920"); fileheadlist.Add("Real Audio (ram)", "2E7261FD"); fileheadlist.Add("Real Media (rm)", "2E524D46"); fileheadlist.Add("MPEG (mpg)", "000001BA"); fileheadlist.Add("MPEG(mpg)", "000001B3"); fileheadlist.Add("Quicktime (mov)", "6D6F6F76"); fileheadlist.Add("Windows Media (asf)", "3026B2758E66CF11"); fileheadlist.Add("MIDI (mid)", "4D546864"); fileheadlist.Add("3GP", "000000146674797033677034"); fileheadlist.Add("MP4", "000000206674797069736F6D"); var vals = from p in fileheadlist where code.StartsWith(p.Value) select p; KeyValuePair<string, string> result = vals.First(); foreach (var i in vals) { if (i.Value.Length > result.Value.Length) result = i; } return result.Key ?? "other"; } }TimFromKhark...
Member
188 Points
127 Posts
Re: FileUpload ContentType Trickery - 2
May 11, 2010 08:31 AM|LINK
Jerry, thanks a lot, but unfortunetely code does not work for 3gp and mp4...
Here are the the codes I'm getting for my test 3gp files:
00186674797033677035000001003367
00186674797033677035000001003367
00146674797033677034000002003367
001C6674797033677034000002006973
001C6674797033677034000002006973
00146674797033677034000002003367
00146674797033677034000002003367
You see, they are different.
Even IE doen't recognize 3gp if you change extension (normally it identify ContentType even if extension is changed manually). Problem seems to be not so easy
Jerry Weng -...
All-Star
29527 Points
3488 Posts
Re: FileUpload ContentType Trickery - 2
May 11, 2010 09:13 AM|LINK
Hi,
As I know, 3gp and mp4 is a very special file type. It is just like a container, there would be different headers for the same extension. But we can find "66747970336770", the bytes from position 3 to position 9 are same, it is tell us the the file type which we can find out from UE. Maybe we need to do some more complicated logic to identity the 3gp and mp4 files.