开发者

Proper use of Response.ContentType

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜