asp C#写csv文件到客户端挂起下载

时间:2015-09-28 20:41:05

标签: asp.net internet-explorer csv httpresponse

我正在尝试将CS​​V文件输出到客户端。当保存对话框出现时(在Internet Explorer 10和Google Chrome中)并且我尝试从浏览器打开它,该文件挂起并停留在"运行安全扫描"。但是,我能够毫无问题地保存文件。以下是我用来生成文件的代码。另外需要注意的是,我尝试了HttpContext.Current.Response.End();HttpContext.Current.ApplicationInstance.CompleteRequest();并获得了相同的结果。请帮忙

        string attachment = "attachment; filename=" + "MyCSVFile.csv";
        var response = HttpContext.Current.Response;

        response.Clear();
        response.ClearHeaders();
        response.ClearContent();
        response.AddHeader("content-disposition", attachment);
        response.ContentType = "text/csv";

        var csv = new StringBuilder();
        // ... Build contents for csv file

        response.Write(csv.ToString());
        response.End(); 

1 个答案:

答案 0 :(得分:0)

显然,这个问题与我的代码无关,而是与Windows中的损坏文件无关。我通过使用Run as Administrator打开命令提示符并运行命令sfc/scannow来解决此问题。它现在可以正确下载和打开。