C# ASP.NET 3.5 内容配置文件下载问题
我有一个存储一些文档的sql数据库。
用户可以登录应用程序并查看其文档列表。
当单击其文档网格视图中的链接按钮下载时,我从数据库获取文件,将其写入文件系统,然后执行此代码。
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"));
我的 GetContentType() 方法仅返回我允许的文件的适当文件类型“application/pdf、application/msw0rd 等。
我的问题是,当文件保存时,它是网页本身,而不是文件中的文件在谷歌浏览器中,它在文件名末尾添加了 .htm 扩展名,我猜是因为它知道这是一个网页?
无论如何,第一步是获取实际文件,而不是副本。他们所在的 HTML 网页
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您如何发送文件的实际内容?
我通常使用 Response.TransmitFile 方法,它基本上打开文件并将其内容发送到 Response.OutputStream
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 Response.OutputStream
而不是
Response.AppendHeader("Content-Disposition",
“内联;” +
“文件名=
使用
Response.AddHeader(“内容处置:”,“附件;文件名=
Instead of
Response.AppendHeader("Content-Disposition",
"inline; " +
"filename=
use
Response.AddHeader("content-disposition:", "attachment;filename=
您是否尝试过将内容处置设置为“附件”而不是“内联”? 我相信浏览器随后会提示用户打开或保存文档。
另外,您通常可以通过使用 BinaryWrite 方法将字节流从数据库直接写入 Response 对象来绕过文件系统...这也是您可能希望在 ASPX 页面上使用 HTTP 处理程序进行调查的情况,因此你不需要担心清除响应等。我过去已经成功地在页面上使用隐藏的 iframe,然后使用 Javascript 将其 src 设置为 HTTP 处理程序,该处理程序将文档 ID 作为参数在查询字符串上。
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.