none
How to determine the content type (MIME) for a file?

    Question

  • Hi all,

     I have an app that I implemented in ASP.NET (Web Forms) that I'm in the process of porting to Silverlight. The app allows a user to upload a file which is stored in a database and can be downloaded later. In the ASP.NET version of the app I'm using System.Web.UI.WebControls.FileUpload to upload the file and I'm able to determine the content type using the PostedFile.ContentType property. I then store the content type in the database along with the file content itself. Then, later when the user downloads the file, an IHttpHandler is used which sets the content type to the correct type. Now, I want to change the app so that this is done through Silverlight on the upload side. The question is, how do I determine the file's content type? I was thinking that maybe it could be done based on the file's extension, but, I'm guessing this wouldn't be possible for platforms like Mac OS that don't use file extensions. As such, I'm guessing there would need to be support for this on the Silverlight side?

    Wednesday, July 08, 2009 4:12 PM

Answers

  • No, there isn't.

    The Silverlight Framework needs to be as small and leightweighted as possible and although it would be useful
    to have such method I wouldn't recommend putting it in, since this is very application specific and would probably be used by only a handful of developers.

    Wednesday, July 08, 2009 5:08 PM

All replies

  • Hey,

     

    unfortunately this is not quite as simple, you see, if there was a common method to determine the purpose and type of a file, we wouldn't need the content type description, would we ?

    The quick and dirty way would indeed be using the files extension and it will work fine on common file types like jpg, png, eml, ... since those have the same extension on all platforms.

    Of course as you said this approach will not work with all files, but at the end of a day all files are nothin but byte arrays, right ?
    If you open a pdf document with a simple editor you will notice that most of the file is unreadable mumbojumbo, but in the first line you'll see the following text "%PDF-1.5". If you open a Zip file in the editor the first 2 symbols will always be "PK" following with a lot of funny symbols...


    This will of course not work for every file, and the syntax for the identifier will differ from file to file.
    This way is the only I can think of how to determine the content type, if additional informations like the extension got lost.

    Wednesday, July 08, 2009 4:35 PM
  • I found this document which says basically what you said. Namely, IE reads the actual file data to determine the type. I'm guessing there's no Silverlight implementation of FindMimeFromData()?

    http://msdn.microsoft.com/en-us/library/ms775147(VS.85).aspx#Known_MimeTypes

    Wednesday, July 08, 2009 4:53 PM
  • No, there isn't.

    The Silverlight Framework needs to be as small and leightweighted as possible and although it would be useful
    to have such method I wouldn't recommend putting it in, since this is very application specific and would probably be used by only a handful of developers.

    Wednesday, July 08, 2009 5:08 PM
  • Where you able to find a work around for this?  It's surprising that in Silverlight drag/drop only supports files, yet there is no consistent way to determine the content type so that you can process the file that has been dropped.

    Wednesday, February 29, 2012 12:23 PM