0

I am trying to serve a csv file from a wcf service, as a string response.

It looks like this:

HttpContext.Current.Response.AddHeader("Content-disposition", "attachment; filename=ImportErrors.csv");
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.Write(myCsvContent);

The response StatusCode is set to (int)HttpStatusCode.OK (200)

It works, but I am only getting 88 bytes of my csv and the rest is cut off (not shown).

Any suggestions on where to look? I don't see any custom entries in my web.config that are setting a limit.

Chris
  • 968
  • 16
  • 27

1 Answers1

0

Make sure to call

HttpContext.Current.Response.Flush();
HttpContext.ApplicationInstance.CompleteRequest();

This will close the stream and flush the rest of the content.

EDIT: Check out this link. It would seem that my original information was a bit out of date... I've been in "MVC Land" for so long I've forgotten this pattern!

Community
  • 1
  • 1
Dave Markle
  • 95,573
  • 20
  • 147
  • 170
  • Response.End() throws an exception for me in the service method, stack trace wouldn't let me see what exactly was the problem - I suspect because this prevents returning whatever return type the contract expects. However, your answer got my problem solved anyway because you mentioned flushing the content. Doing Response.Flush() after each line did not throw exception but allowed all of the data to come across. – Chris Dec 17 '12 at 20:57
  • Thanks. Post edited. I never can remember if you need to call Flush() before or not, for some reason. There's not enough documentation on "why" sometimes... – Dave Markle Dec 17 '12 at 21:34
  • I'm surprised. Flush should never be needed except for ajax-comet style responses... – usr Dec 17 '12 at 22:04