Capture file download

We have a strange issue with file suggestion in our ASP.NET server.

If the user clicks on the link, we want to have a file download dialog. No WMP open for WMV, no Adobe open for PDF, etc.

For this, we use the following HTTP handler, which jumps to WMV, PDF, etc.

    public void ProcessRequest(HttpContext context)
    {
        // don't allow caching
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        context.Response.Cache.SetNoStore();
        context.Response.Cache.SetExpires(DateTime.MinValue);

        string contentDisposition = string.Format("attachment; filename=\"{0}\"", Path.GetFileName(context.Request.PhysicalPath));
        string contentLength;

        using (FileStream fileStream = File.OpenRead(context.Request.PhysicalPath))
        {
            contentLength = fileStream.Length.ToString(CultureInfo.InvariantCulture);
        }

        context.Response.ContentType = "application/octet-stream";
        context.Response.AddHeader("Content-Disposition", contentDisposition);
        context.Response.AddHeader("Content-Length", contentLength);
        context.Response.AddHeader("Content-Description", "File Transfer");
        context.Response.AddHeader("Content-Transfer-Encoding", "binary");
        context.Response.TransmitFile(context.Request.PhysicalPath);
    }

      

Sniffing with a fiddler, these are the actual headers sent:

HTTP/1.1 200 OK
Cache-Control: no-cache, no-store
Pragma: no-cache
Content-Length: 8661299
Content-Type: application/octet-stream
Expires: -1
Server: Microsoft-IIS/7.5
Content-Disposition: attachment; filename="foo.wmv"
Content-Description: File Transfer
Content-Transfer-Encoding: binary
X-Powered-By: ASP.NET
Date: Wed, 04 Apr 2012 09:38:14 GMT

      

However, this still opens WMP when we click on the WMV link, same for Adobe Reader, it still opens Adobe Reader inside IE window.

This issue doesn't seem to be present in Firefox, however it does occur on IE8 (32-bit) on Windows 7 (32-bit).

Any help?

+3


source to share


1 answer


replace

context.Response.ContentType = "application/octet-stream";

      

from



context.Response.ContentType = "application/force-download";

      

and see what it does, don't know if it works for all browsers.

+5


source







All Articles