【问题标题】:CSV file includes the current page html in the fileCSV 文件包含文件中的当前页面 html
【发布时间】:2016-11-03 10:33:00
【问题描述】:

我正在使用下面的代码单击 a 创建一个 csvfile,但是当我打开文件时,它具有正确的数据以及当前页面的 html。

private void ExportToCsvFile(List<LeadInfo> leadInfos)
        {
            const string fileName = "lead_Report.csv";

            //Convert the List to csv string
            var result = leadInfos.ToCsv();


            //Write byte[] to the response or Start downloading the file automatically
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
            HttpContext.Current.Response.ContentType = "text/csv";
            HttpContext.Current.Response.AddHeader("Pragma", "public");
            HttpContext.Current.Response.Write(result);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }

我在这里缺少的任何东西。非常感谢任何帮助

【问题讨论】:

  • 我猜这取决于传递给leadInfos的内容
  • 发布您的控制器操作,您可能在控制器操作结束时调用return View(...)
  • 我在 asp,net webform 中点击按钮调用这个
  • 似乎 Response.end() 是唯一的解决方案,因为 HttpContext.Current.ApplicationInstance.CompleteRequest();不工作

标签: c# asp.net csv export-to-csv


【解决方案1】:

我尝试使用 Response.End() 而不是 CompleteRequest()。它的工作原理

HttpContext.Current.Response.Write(result);
HttpContext.Current.Response.End();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    • 2020-02-25
    • 1970-01-01
    相关资源
    最近更新 更多