开发者

Proper use of Response.ContentType

开发者 https://www.devze.com 2023-04-07 04:54 出处:网络
I have a site that allows us开发者_开发技巧ers to upload a document such as pdf, word, excel etc.

I have a site that allows us开发者_开发技巧ers to upload a document such as pdf, word, excel etc.

Now in my code, when I want to show the document in a browser, I am checking for the extension and setting the ContentType appropriately.

However, this switch statement just doesn't sound like the best solution.

Is there a way I can set the content type based on the Stream?


If you don't want to use switch like statements, use Dictionary to add possible MIME-entries.

Dictionary<string, string> map=new Dictionary<string, string>
{
  {"txt", "text/plain"},                                                
  {"exe", "application/octet-stream"},
  {"bmp", "image/bmp"}
};

and set content type,

Response.ContentType=map[ext];

I've found an interesting stackoverflow thread - Using .NET, how can you find the mime type of a file based on the file signature not the extension

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号