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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With