1

I'm having trouble wrapping my head around how to do this.

I have a DataTable being returned via stored procedure. I'm then going through it and creating a delimited file based off of the information returned.

I've made it here without any issues.

My problem is taking my StringBuilder object and creating a CSV file with it and then sending it to the user to download. I can't create a temporary file on the server due to permissions so I'm trying to do this all in memory.

I'm thinking I need to do something with a MemoryStream to create the "file" and then serving it up with Response but I'm not sure what steps to take.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Jabsy
  • 171
  • 4
  • 16
  • possible duplicate of [How to return text to the client as html page?](http://stackoverflow.com/questions/9983859/how-to-return-text-to-the-client-as-html-page) – David Apr 04 '12 at 01:16
  • This one's been answered a few times here :) Essentially you want to set some headers (specifically `content-disposition`, to tell the browser that the response is a file), write the file (or the string, rather) to the output, and close the output. Good luck! – David Apr 04 '12 at 01:17
  • Combine these http://stackoverflow.com/questions/4884357/converting-dataset-datatable-to-csv – Micah Armantrout Apr 04 '12 at 01:20

2 Answers2

4
Response.Clear();
Response.ContentType = "application/CSV";
Response.AddHeader("content-disposition", "attachment; filename=\"" + filename + ".csv\"");
Response.Write(csvBuilder.ToString());
Response.End();
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

I'm pretty sure that this answer will get you on the right track.

Make sure you set the contentType to the appropriate values for your file. Most likely response.ContentType = "text/plain";

Setting PDF file name dynamically

Community
  • 1
  • 1
Paul Farry
  • 4,730
  • 2
  • 35
  • 61