Dynamically Creating Text Files

G

Guest

Hi all,

I'm trying to create a text file on the fly for a user of an ASP.NET
application to download. So far the code I have is:

MemoryStream oStream = null; // using System.IO
string contenttype = "text/plain";
string contentdisposition = "filename=report.txt";
oStream = new MemoryStream();
StreamWriter sw = new StreamWriter(oStream);
Response.Clear();
sw.WriteLine("Hello");
Response.ContentType = contenttype;
Response.Buffer= true;
Response.AddHeader("Content-Disposition", contentdisposition);
Response.BinaryWrite(oStream.ToArray());
Response.End();

However I seem to be getting "empty html" instead. Further, the debugger
shows the memorystream as empty when I call writeline on sw.

Any ideas?

Spammy
 
E

Eliyahu Goldin

Likely the problem is in not closing the stream. Call sw.Close() after
writing "Hello".

Eliyahu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top