Content Type when serving a file in a generic handler

I’m busy writing a handler to serve various documents for download or presentation in web forms pages. The documents range from various image formats, to PDF, to MS Office documents, to generic binaries. My basic draft of the download process is as below:

public void ProcessRequest(HttpContext context)
{
    var docUrl = context.Request["docUrl"];
    if (string.IsNullOrEmpty(docUrl)) {
        context.Response.End();
        return;
    }
    var docPath = context.Server.MapPath(docUrl);
    var docInfo = new FileInfo(docPath);

    context.Response.Clear();
    context.Response.AddHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(docPath));
    context.Response.AddHeader("Content-Length", docInfo.Length.ToString());
    context.Response.ContentType = "application/octet-stream";
    context.Response.WriteFile(docPath);
    context.Response.End();
}

However, I have some misgivings about lumping all documents together as application/octet-stream, and I would prefer, if feasible, to use a more specific content type per document type. I have a DB table for document types where I could store this. Am I going in the right direction, and if so, where can I find a suitable starting list of content types for document types?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

application/octet-stream is fine for file downloading, but if you want the browser to interact with the file, you might want to change the content type.

For example, the mime-type for downloading a PDF file is application/octet-stream, while application/pdf will tell the browser to open the file in the browser itself.

http://en.wikipedia.org/wiki/Internet_media_type


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x