WCF字符串方法,也用response.write提供下载 - 目前只在IE中工作

时间:2012-12-18 16:03:44

标签: c# .net wcf upload download

我有这样的WCF合同:

[OperationContract]
[WebInvoke(Method = "POST")]
string Import_CSV();

然后Import_CSV()方法成功接受来自HttpContext.Current.Request.Files的文件上传... Import_CSV()方法返回一个字符串,其中包含成功消息或发生的特定错误。

但是,如果我对上传的文件执行的服务器操作存在多个错误,那么我的目标是生成一个csv文件并将其提供给用户以供他们下载。

所以,我创建了csv内容,现在我想显示它供用户下载。

将Statuscode设置为(int)HttpStatusCode.OK,然后我返回一个空字符串以获得成功,并使用response.write来提供新生成的csv文件,如下所示:

HttpContext.current.Response.ContentType = "text/csv";
HttpContext.current.Response.AddHeader("Content-Disposition", "attachment;filename=ImportErrors.csv");
HttpContext.current.Response.AddHeader("Pragma", "no-cache");
HttpContext.current.Response.AddHeader("Cache-Control", "no-cache");
foreach (var line in csvLines)
{
    context.Response.Write(line);
    context.Response.Write(Environment.NewLine);
    context.Response.Flush();
}
context.ApplicationInstance.CompleteRequest();

在互联网浏览器中,一切似乎都按计划运行,但Firefox& Chrome似乎不知道如何处理这些数据。我尝试过为内容设置不同的http状态代码和不同的mime类型,但我怀疑我走的是错误的道路。

有什么建议吗?

我最近在堆栈溢出中问了一小部分这个难题:Limit of 88 bytes on response.write?

答案非常有用,但现在我的问题是关于WCF,通过此服务调用返回文本和文件,并且需要的不仅仅是IE。谢谢!


在chrome中使用标题:

请求:

POST /services/PTService.svc/Import_CSV HTTP/1.1
Host: inf18
Connection: keep-alive
Content-Length: 565
Origin: http://inf18
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 
Safari/537.11
Content-Type: multipart/form-data; boundary=----WebKitFormBoundarykpa0b9lHil712wdL
Accept: */*
Referer: http://inf18/dashboard/ManageRecipient.aspx
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: ASP.NET_SessionId=o50tom0nkjfmglumcmrbtlds

响应:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Transfer-Encoding: chunked
Content-Type: text/csv; charset=utf-8
Content-Encoding: gzip
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
Content-Disposition: attachment;filename=ImportErrors.csv
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 18 Dec 2012 18:30:37 GMT

IE中使用的标题:(IE9 Compat。视图,IE9标准)

请求:

POST /services/PTService.svc/Import_CSV HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-
ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer: http://inf18/dashboard/ManageRecipient.aspx
Accept-Language: en-US
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR
2.0.50727;     .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MDDR)
Content-Type: multipart/form-data; boundary=---------------------------7dc180222c0fb0
Accept-Encoding: gzip, deflate
Host: inf18
Content-Length: 598
Connection: Keep-Alive
Pragma: no-cache
Cookie: ASP.NET_SessionId=e2iwodwyy2muxoj4zhpisl5k

响应:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Transfer-Encoding: chunked
Content-Type: text/csv; charset=utf-8
Content-Encoding: gzip
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
Content-Disposition: attachment;filename=ImportErrors.csv
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 18 Dec 2012 18:33:34 GMT

1 个答案:

答案 0 :(得分:1)

您是否尝试在写入响应之前添加以下行?

/******************************************/
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Clear();
/******************************************/

HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=ImportErrors.csv");
//rest of your code sample