In C#, how can I know the file type from a byte[]?

Viewed 81686

I have a byte array filled from a file uploaded. But, in another part of the code, I need to know this file type uploaded from the byte[] so I can render the correct content-type to browser!

Thanks!!

10 Answers

If you know extension of the file name, may be System.Web.MimeMapping will do the trick:

MimeMapping.GetMimeMapping(fileDisplayNameWithExtension)

I used it in MVC Action like this:

return File(fileDataByteArray, MimeMapping.GetMimeMapping(fileDisplayNameWithExtension), fileDisplayNameWithExtension);
Related