views:

5484

answers:

3

I have a sql database that stores some documents.

A user can sign into the application, and view a list of their documents.

When clicking a linkbutton download in a gridview of their docs, I get the file from the database, write it to the file system, and then execute this code.

    System.IO.FileInfo file = new System.IO.FileInfo(System.Configuration.ConfigurationManager.AppSettings["UploadPath"] + DocumentName);

    Response.Clear();
    Response.ClearContent();
    Response.ClearHeaders();
    Response.Cookies.Clear();
    Response.Cache.SetCacheability(HttpCacheability.Private);
    Response.CacheControl = "private";
    Response.Charset = System.Text.UTF8Encoding.UTF8.WebName;
    Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
    Response.AppendHeader("Content-Length", file.Length.ToString());
    Response.AppendHeader("Pragma","cache");
    Response.AppendHeader("Expires", "60");
    Response.ContentType = GetContentType(file.Extension);
    Response.AppendHeader("Content-Disposition",
    "inline; " +
    "filename=\"" + file.Name + "\"; " +
    "size=" + file.Length.ToString() + "; " +
    "creation-date=" + DateTime.Now.ToString("R") + "; " +
    "modification-date=" + DateTime.Now.ToString("R") + "; " +
    "read-date=" + DateTime.Now.ToString("R"));

My GetContentType() method just returns the appropriate file type for the files I'm allowing "application/pdf, application/msw0rd, etc.

My problem is that when the file gets saved, it's the webpage itself, not the file from the file system. And in google chrome, it's putting a .htm extension on the end of the filename, I guess because it knows it's a web page?

Anyhow, a great first step would be to get the actual file, and not a copy of the web page in HTML they are sitting on!

Thanks.

+1  A: 

Have you tried setting the content-disposition to "attachment" rather than "inline"? I believe that the browser will then prompt the user to open or save the document.

Also, you can usually bypass the file system by writing your byte stream from the database directly to the Response object with the BinaryWrite method... This is also a case where you might want to investigate using an HTTP Handler instead on an ASPX page so that you don't need to worry about clearing the Response, etc. I have had success in the past with using a hidden iframe on the page, then using Javascript to set its src to an HTTP Handler that takes the document ID as a parameter on the QueryString.

+3  A: 

Instead of

Response.AppendHeader("Content-Disposition", "inline; " + "filename=

use

Response.AddHeader("content-disposition:", "attachment;filename=

Samiksha
+5  A: 

How are you sending the actual content of the file??

I usually use Response.TransmitFile method, it basically opens the file and sends its content to the Request.OutputStream

CMS