Problems loading files with C # ASP.NET 3.5 content

I have a sql database in which some documents are stored.

The user can enter the application and view a list of his documents.

When I click on the link to the link in the form of a grid of their documents, I get the file from the database, write it to the file system and 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 simply returns the appropriate file type for the files that I allow "application / pdf, application / msw0rd, etc.

My problem is that when the file is saved, it is the web page itself, not the file from the file system. And in google chrome, it puts the .htm extension at the end of the file name, I think, because it knows it as a web page?

In any case, the first first step would be to get the actual file, not a copy of the webpage in HTML they are sitting on!

Thanks.

+6
c # content-disposition
source share
3 answers

How do you send the actual contents of a file?

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

+9
source share

Instead

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

using

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

+9
source share

Have you tried setting the content to β€œbinding” rather than β€œbuilt-in”? I believe that the browser will prompt the user to open or save the document.

Alternatively, you can usually bypass the file system by writing a stream of bytes from the database directly to the Response object using the BinaryWrite method ... This is also the case when you may need to investigate using an HTTP handler instead of ASPX, so you don't have to worry about clearing the answer, etc. I have had success in the past using a hidden iframe on the page and then using Javascript to set its src to an HTTP handler that takes the document id as a parameter in a QueryString.

+3
source share

All Articles