I have a WCF contract like this:
[OperationContract]
[WebInvoke(Method = "POST")]
string Import_CSV();
And then the Import_CSV() method successfully accepts a file upload from HttpContext.Current.Request.Files... The Import_CSV() method returns a string with message of success or the specific error that occurred.
However, if there are multiple errors with the server operation that I perform on the file that was uploaded, then my goal is to generate a csv file and serve it to the user for them to download.
So, I created csv content, and now I want to display it for the user to download.
Setting the Statuscode to (int)HttpStatusCode.OK, I then return an empty string for success, and use response.write to serve the newly generated csv file like this:
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();
In internet explorer, everything seems to work as planned, but Firefox & Chrome don't seem to know what to do with this data. I have tried setting different http status codes, and different mime types for the content, but I suspect I am headed down the wrong road.
Any suggestions?
I asked a smaller piece of this puzzle recently on stack overflow here: Limit of 88 bytes on response.write?
Answer was very helpful, but now my question is regarding WCF, returning both text and file with this service call, and needs to work in more than just IE. Thanks!
Headers when used in chrome:
request:
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
response:
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
Headers when used in IE: (IE9 Compat. view, IE9 Standards)
request:
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
response:
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