My ASP.NET MVC application has pages with attachments and these attachments can be of many different file types.
When the user then wants to access their attachment, I need to fire off an FileResult and return the file attachment which I have the path to.
However, I have no database of mime-types, nor do I know off-hand the mime-type of these files off hand.
What is the proper way to handle this? Is there a way I can return a file and let the framework attempt to figure out the mimetype?
Any suggestions?
The way I’ve done it is by keeping a list of well-known extensions and their mime types, and if the extension isn’t found then just return it as
application/octet-stream. The reason for this is that this mime type is applied to applications (e.g., exe) which the browser, depending on security settings, may allow you to pass to the operating system, thus opening the default editor for that file type. BTW, consider the security implications for every type of file you may accept and transfer to users.Here’s the list I generally use:
Here’s an example of how to use this (c#-like pseudocode).