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
精彩评论