【问题标题】:Network Failure when using Response.TransmitFile使用 Response.TransmitFile 时出现网络故障
【发布时间】:2020-01-21 05:51:25
【问题描述】:

我正在尝试使用以下代码,以允许用户下载位于本地服务器上的 excel:

FileInfo dest_file= new FileInfo(filename);
Response.ClearContent();
Response.ContentType = "Excel/xls";
Response.AddHeader("Content-Disposition", string.Format("attachment; filename = {0}", filename));
Response.AddHeader("Content-Length", dest_file.Length.toString());
Response.TransmitFile(filename);
Response.End();

但是,在发布解决方案后,下载文件会导致网络故障。知道如何解决这个问题吗?

请注意,通过目录浏览尝试访问文件时,文件下载成功。

【问题讨论】:

    标签: c# .net download response


    【解决方案1】:

    您似乎使用了无效的内容类型

    对于 xlsx 使用这个

    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    

    对于 xls

    Response.ContentType = "application/vnd.ms-excel"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-30
      • 1970-01-01
      • 2016-12-17
      • 1970-01-01
      • 2018-02-14
      • 2016-08-08
      • 1970-01-01
      相关资源
      最近更新 更多