C# ASP.NET 3.5 content-disposition 文件下载问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/336345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# ASP.NET 3.5 content-disposition file download problems
提问by John Batdorf
I have a sql database that stores some documents.
我有一个存储一些文档的 sql 数据库。
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.
在他们文档的 gridview 中单击链接按钮下载时,我从数据库中获取文件,将其写入文件系统,然后执行此代码。
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.
我的 GetContentType() 方法只是为我允许的文件返回适当的文件类型“application/pdf、application/msw0rd 等。
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?
我的问题是,当文件被保存时,它是网页本身,而不是来自文件系统的文件。在谷歌浏览器中,它在文件名的末尾放置了一个 .htm 扩展名,我猜是因为它知道它是一个网页?
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!
无论如何,第一步是获取实际文件,而不是他们所在的 HTML 网页副本!
Thanks.
谢谢。
采纳答案by CMS
How are you sending the actual content of the file??
你如何发送文件的实际内容?
I usually use Response.TransmitFilemethod, it basically opens the file and sends its content to the Response.OutputStream
我通常使用Response.TransmitFile方法,它基本上打开文件并将其内容发送到 Response.OutputStream
回答by user19371
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.
此外,您通常可以通过使用 BinaryWrite 方法将字节流从数据库直接写入 Response 对象来绕过文件系统...这也是您可能希望在 ASPX 页面上使用 HTTP 处理程序进行调查的情况您无需担心清除响应等。我过去在页面上使用隐藏的 iframe 取得了成功,然后使用 Javascript 将其 src 设置为以文档 ID 作为参数的 HTTP 处理程序在 QueryString 上。
回答by Samiksha
Instead of
代替
Response.AppendHeader("Content-Disposition", "inline; " + "filename=
Response.AppendHeader("Content-Disposition", "inline; " + "filename=
use
用
Response.AddHeader("content-disposition:", "attachment;filename=
Response.AddHeader("content-disposition:", "attachment;filename=