Joh*_*orf 6 c# asp.net content-disposition
我有一个存储一些文件的SQL数据库.
用户可以登录该应用程序,并查看其文档列表.
当在文档的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"));
Run Code Online (Sandbox Code Playgroud)
我的GetContentType()方法只返回我允许的文件的相应文件类型"application/pdf,application/msw0rd等.
我的问题是,当文件被保存时,它是网页本身,而不是文件系统中的文件.在谷歌浏览器中,它在文件名的末尾添加.htm扩展名,我猜是因为它知道它是一个网页?
无论如何,一个伟大的第一步是获取实际文件,而不是他们所坐的HTML网页的副本!
谢谢.
代替
Response.AppendHeader("Content-Disposition","inline;"+"filename =
使用
Response.AddHeader("content-disposition:","attachment; filename =