0

Using code such as:

    HttpContext.Current.Response.Clear()
    HttpContext.Current.Response.Buffer = True
    HttpContext.Current.Response.ContentType = "application/CSV"
    HttpContext.Current.Response.AppendHeader("content-disposition", "attachment;filename=FileName.csv")
    HttpContext.Current.Response.Charset = ""
    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache)
    HttpContext.Current.Response.Write(csvString.ToString())
    HttpContext.Current.Response.[End]()

Can I serve a CSV file from a web service call. It doesn't seem to work what am I missing?

I have set up a traditional asp:button and this serves the file correctly, though the web service returns a 500 server error Thread was being aborted

DavidB
  • 2,566
  • 3
  • 33
  • 63

1 Answers1

0

you can return a byte array for files. Read up on Accept and Return file to/from C# WebService

That being said, and this is personal taste, I like to keep my web services "clean" in that they only return XML. They are, after all, for inter-machine communication and file transfer has its own protocol in FTP so there's that. Behavior like what you're looking for should, as far as I feel, be isolated into a generic (ashx) handler that can be called whenever needed. Again, that's taste.

an excerpt from an ashx handler could look like this:

context.Response.ContentType = "text/CSV";
context.Response.AddHeader("Content-disposition", "attachment; filename=\"" 
   + fileName +  "\"");
context.Response.Clear();
context.Response.BinaryWrite(fileBytes);
context.Response.Flush();
Community
  • 1
  • 1
Wim Ombelets
  • 5,097
  • 3
  • 39
  • 55