how do i create a new file -- in memory?

M

matt

hello,

i have what i would guess to be a relatively "newbie" question: how do
i create & write to a new text file?

i have found plenty of articles (MSDN & other) on how to do this in
conjuction w/ the *file system*, but i do not wish to touch the
client's hard drive. rather, id like to build up a .txt file in memory,
then Response.WriteFile() it back to the user (ASP.NET plaform).

there are so many streams it's somewhat confusing. a high-level point
or link to an article would be most helpful.


thanks!
matt
 
G

Guest

(e-mail address removed) wrote in @z14g2000cwz.googlegroups.com:
i have found plenty of articles (MSDN & other) on how to do this in
conjuction w/ the *file system*, but i do not wish to touch the
client's hard drive. rather, id like to build up a .txt file in memory,
then Response.WriteFile() it back to the user (ASP.NET plaform).

there are so many streams it's somewhat confusing. a high-level point
or link to an article would be most helpful.

You can use a MemoryStream instead of a FileStream.

You should also use StringBuilder for building large strings. Regular
string concatenation is ***really*** slow (especially if the text file gets
large).
 
M

matt

ok, so i can use this syntax to write:

MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);

foreach (DataRow row in _dtResults.Rows)
{
sw.WriteLine("foo");
}

...got that part. but then what do to change that memorystream into a
file? the Response.WriteFile() method takes in either a file, or a
"IntPtr fileHandler", which i am not familar with.

is there a way to convert a stream into a new file? the File
constructor doesnt seem to take a stream as a parameter.


thanks!
 
M

matt

ah.. ok, i dont think the memorystream is really needed. atleast in my
case... since i am return this to the web user, i can do so:

//build file's contents
StrinbBuilder sb = new StringBuilder(50);

foreach (DataRow row in _dtResults.Rows)
{
sb.Append("foo");
sb.Append("\n");
}

//send file to user
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AppendHeader("content-disposition", "attachment;
filename=foo.text");
Response.Flush();
Response.Write(fileContents);
Response.End();


(tho i am still curious about how one takes a memorystream and turns it
into a file object)

matt
 
G

Guest

(e-mail address removed) wrote in @g14g2000cwa.googlegroups.com:
ah.. ok, i dont think the memorystream is really needed. atleast in my
case... since i am return this to the web user, i can do so:

//build file's contents
StrinbBuilder sb = new StringBuilder(50);

foreach (DataRow row in _dtResults.Rows)
{
sb.Append("foo");
sb.Append("\n");
}

//send file to user
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AppendHeader("content-disposition", "attachment;
filename=foo.text");
Response.Flush();
Response.Write(fileContents);
Response.End();

Yes this works great : )
(tho i am still curious about how one takes a memorystream and turns it
into a file object)

Use a StreamWriter - pass the memorystream as a parameter in the
contrustor of the StreamWriter:

http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/cpref/html/frlrfsystemiostreamwriterclassctortopic1.asp

In the above example, the StreamWriter is passed a FileStream. In your
case, you would pass a MemoryStream to the StreamWriter.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top