我有一个存储一些文档的 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 网页副本!
谢谢。