错误的文件正在下载服务器到客户端

时间:2012-11-23 09:52:49

标签: download webforms

尝试从服务器下载文件到客户端时遇到问题。单击保存文件提示符显示它应该是,但它不指向我要下载的文件,而是指向我的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();
 }

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

问题是“内联”。还有一些其他的东西要调整以使代码更容易阅读:

相关文章:Content-Disposition:What are the differences between "inline" and "attachment"?

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();
}