Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am downloading file from the database by carrying out the extension of the file and checking the extension with if-else and with different response.contenttype....according to extension.
here is my code:

C#
if (ext == ".pdf")
           {
               Response.ContentType = "application/pdf";
           }

           else if (ext == ".doc" || ext == ".rtf" || ext == ".docx")
           {
               Response.ContentType = "Application/msword";
           }

           else if (ext == ".txt")
           {
               Response.ContentType = "text/Plain";
           }

           else if (ext == ".rar")
           {
               Response.ContentType = "Application/x-rar-compressed";
           }
           else if (ext == ".jpeg" || ext==".jpg")
           {
               Response.ContentType = "image/jpeg";
           }
           else
           {
               Response.ContentType = "Application/octetstream";
           }

           Response.AddHeader("Content-disposition", "attachment; filename=" + filename);
           Response.BinaryWrite(byteimg);
           Response.Flush();
           Response.End();
       }


So, instead of declaring response.contenttype according to extension each and every time at the time of downloading, i want the common response.contentype to exclude all these one's/.....please help..
Thanks..:)
Posted

You want to change how the entire internet works. You can zip the files, and thus set the type to be a zip, but otherwise, you need to tell the browser what the content type is, so it knows what to do with it.
 
Share this answer
 
There are generic "octet" MIME types, but they are hardly useful in your case.

Did it dawn on you that there must be a better way than using if/else constructs? You could just put the extensions and MIME types in a Dictionary and just do a lookup of the extension you're sending to the client to get it's an appropriate MIME type.
 
Share this answer
 
Comments
Pranav-BiTwiser 15-Mar-14 2:17am    
what if i use...
Response.ContentType = "Application/octetstream";
Dave Kreskowiak 15-Mar-14 9:57am    
Yes, you can. What will happen on the browser side is that the user will be prompted where they want to save the file. It will not open in the browser because the MIME type is what tells the browser how to handle the file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900