Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Content Type when serving a file in a generic handler

Tags:

asp.net

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?

like image 584
ProfK Avatar asked Dec 10 '10 05:12

ProfK


1 Answers

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

like image 101
Bonshington Avatar answered Oct 12 '22 12:10

Bonshington