Question stream code-generated 'file' to client browser

dejswa

Member
Joined
Jun 29, 2010
Messages
11
Programming Experience
1-3
Happy July 4th & thanks for reading.

I am using VS 2010 to produce .aspx code in framework 3.5 to run on IIS (6- I believe).

I want to do the equivalent of response.writefile("hello.txt") where hello.txt is a local file on the server.

HOWEVER, I need to generate the file stream through code running on IIS, that is, I will not be extracting the data from a file.

For example, I might want to generate a small text file containing "hello, world". I need to have the client click a browser button, activating a server process that appears to send a file to the client. Actually I will be sending .csv data which will represent an excel spreadsheet, thus I will need to tell the client the name of the file, "hello.xls", generate the csv code by looping through some database items, then flush or terminate the stream.

I do not need any fancy formatting and do not want to use any excel 'objects' or special excel 'includes', etc. Just want to tell the client the file name to use and generate and send the data.

Sorry, I know the answer is out here somewhere but I just cannot seem to search for it properly. I keep running into load of info on streaming files using response.write, response.transmitfile, and so on.

I need something like . . . pseudo code here

'set up database dataset here . . .
response.beginfilestream("junk.xls") ' tell client file name to create
response.sendstreamdata(any_special_headers_for_this_file_type)
response.sendstreamdata("'Column 1', 'Column 2', 'Column 3'" & CRLF)
do while myreader.read()
response.sendstreamdata("'" & myreader.item(0) & "', " & myreader.item(1) & "', " & myreader.item(1) & "'" & CRLF)
loop
response.sendstreamdata(any_special_file_footers)
response.endstreamdata 'tells client we are done

As I mentioned, response.writefile works just fine, but I need to generate the data in real time, not pull it off a file.

Many thanks,

DJ
 
Thanks, John. This may be the answer. Right in front of me all the time using response.write.

I tried this

Protected Sub btncsv_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btncsv.Click
Response.Clear()
Response.ContentType = "application/txt"
Response.AddHeader("content-disposition", "attachement; filename=mycsv.txt")
Response.Write("this is the data")
Response.End()
End Sub

and the client browser opens it as a plain text file. Now I will try and create some spreadsheet data and let you know.

DJ
 
Back
Top