1

I have set the Response.ContentType = "application/octet-stream"; in the Page_Load handler of the Downloads.aspx page. when I try to download a .gif file it works fine. But in the case of a .jpg file the dialog shows that the file type is "application/octet-stream" and the downloaded file is simply a file which is not .jpg as expected.

Md. Arafat Al Mahmud
  • 3,124
  • 5
  • 35
  • 66
  • Although a better CT could be used, also consider the `Content-Disposition` header to set the filename .. –  Aug 08 '12 at 19:12

1 Answers1

4

Try setting explicit content type for each file type.

switch (fileExtension)
{
    case "gif": Response.ContentType = "image/gif"; break; 
    case "jpeg": Response.ContentType = "image/jpg"; break; 
    case "jpg": Response.ContentType = "image/jpg"; break; 
    case "png": Response.ContentType = "image/png"; break; 

    default: Response.ContentType = "application/octet-stream"; break; 
}

Refer following links for mime-type reference:

http://www.freeformatter.com/mime-types-list.html
https://github.com/cymen/ApacheMimeTypesToDotNet/blob/master/ApacheMimeTypes.cs
Firoz Ansari
  • 2,505
  • 1
  • 23
  • 36
  • its good idea, but isn't it cumbersome ? How could I know there is no other file extension which falls in 'default' and doesn't open accurately ? – Md. Arafat Al Mahmud Aug 08 '12 at 19:16
  • 1
    You should take care of what your users can download from the site, allowing to download only a small subset of filetypes – tanathos Aug 08 '12 at 19:28
  • @tanathos I'm building a community site where anyone will be able to upload and download any type of file – Md. Arafat Al Mahmud Aug 08 '12 at 19:33
  • @Arafat, You can refer http://msdn.microsoft.com/en-us/library/ms775147.aspx for all known MIME types. I will surely provide content type of all common file types and for rest, I fall-back to octet-steam. – Firoz Ansari Aug 08 '12 at 19:57
  • @Arafat, I just googled it and found code snippet which has more extensive handle for mime type. http://stackoverflow.com/questions/1029740/get-a-mime-from-an-extension – Firoz Ansari Aug 08 '12 at 20:06