无法从Web方法导出CSV文件

时间:2015-10-20 15:51:02

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

我试图从网络方法下载CSV文件 但我得到"服务器无法在发送HTTP标头后附加标头"调用服务器函数后出错 我该如何解决?

这是客户端代码

 $.ajax({
          contentType: 'application/json; charset=utf-8',
          dataType: 'json',
          type: 'POST',
          url:'WebMethods/ExportErrorsService.asmx/ExportErrors'});

服务器端:

   [WebMethod(EnableSession = true)]
    public void ExportErrors()
    {
        var attachment = "attachment; filename=test.csv";
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.AddHeader("content-disposition", attachment);
        HttpContext.Current.Response.ContentType = "text/csv";
        HttpContext.Current.Response.AddHeader("Pragma", "public");

        HttpContext.Current.Response.Write("MyTest1");
        HttpContext.Current.Response.Write("MyTest2");
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.End();
    }

0 个答案:

没有答案