Generate a file from a string and send to the user C#

Go To StackoverFlow.com

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.

2012-04-04 01:10
by Jabsy
possible duplicate of How to return text to the client as html page?David 2012-04-04 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 2012-04-04 01:17
Combine these http://stackoverflow.com/questions/4884357/converting-dataset-datatable-to-cs - Micah Armantrout 2012-04-04 01:20


4

Response.Clear();
Response.ContentType = "application/CSV";
Response.AddHeader("content-disposition", "attachment; filename=\"" + filename + ".csv\"");
Response.Write(csvBuilder.ToString());
Response.End();
2012-04-04 01:19
by Rango


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

2012-04-04 01:19
by Paul Farry
Ads