尝试将文件从服务器下载到客户端时遇到问题。单击时,保存文件提示符显示得像它应该的那样,但它不指向我要下载的文件,而是指向我的 aspx 页面?换句话说,它不会下载我要下载的文件,而是下载下载链接所在的页面。真的很奇怪...似乎好像我指定下载的文件被完全忽略/没有效果......
if (File.Exists(Server.MapPath(driversLocation + name + ".zip")))
{
FileInfo fileInfo = new FileInfo(Server.MapPath(driversLocation) + name + ".zip");
Response.Clear();
Response.AddHeader("Content-Disposition", "inline;attachment; filename=" + name + ".zip");
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.ContentType = "application/download";
Response.Flush();
Response.TransmitFile(Server.MapPath(driversLocation) + name + ".zip");
Response.End();
}
任何帮助将不胜感激!
问题是"内联"。还有一些其他事情需要调整,以使代码更易于阅读:
延伸阅读》内容处置:"内联"和"附件"有什么区别?
FileInfo fileInfo = new FileInfo(Server.MapPath(driversLocation + name + ".zip"));
if (fileInfo.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name);
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.ContentType = "application/x-zip-compressed";
Response.TransmitFile(fileInfo.FullName);
Response.End();
}