Best practice for streaming fairly large files from server to clie

  • Thread starter Thread starter Phil Johnson
  • Start date Start date
P

Phil Johnson

Hello,

I have an issue with some code I am updating:

An entire file is loaded into a memory stream then that memory stream uses
its WriteTo method to write to the responses output stream. With larger
files (250 mb) I get a system out of memory exception because the whole file
is loaded into RAM.

I need to modify this so the file is buffered into the output stream (maybe
via a filestream?) to avoid the whole file being loaded into ram.

Does anybody have any good example code or links of how to do this?

Thanks in advance for any help.
 
One possible option is to redirect the browser to the file location itself.
If the file is stored in a non-http accessible folder, you could copy it
into a temp folder for download. Of course, then you have to clean it up at
some point and there is a potential security risk as other users could also
download the file.

Barring that, I think what you said would be fine. Just read chunks from a
FileStream and buffer them to the Response.

Scott
 
Thanks for your thoughts Scott...

It looks like buffering the filestream in chunks is going to be the best
option... the files will be temporary files and I need to delete them as soon
as the data has been streamed.

--
Regards,

Phil Johnson (MCAD)


Scott Roberts said:
One possible option is to redirect the browser to the file location itself.
If the file is stored in a non-http accessible folder, you could copy it
into a temp folder for download. Of course, then you have to clean it up at
some point and there is a potential security risk as other users could also
download the file.

Barring that, I think what you said would be fine. Just read chunks from a
FileStream and buffer them to the Response.

Scott
 
Hi sloan, thanks for the sample.

I think I am trying to do something different though, correct me if Im wrong
though...

Your sample looks like it is a client and streams a file from a url to a
local file.

What I am trying to do is to create a server that has a file stored locally
and streams that file to the client.

--
Regards,

Phil Johnson (MCAD)


sloan said:
http://groups.google.com/group/microsoft.public.dotnet.framework.aspnet/msg/5d13224a77b89add


See if the WriteTextFile in the HttpHelper will help you.


If it does, please post back here.

I ~think that is what you're looking for.
 
first turn of buffering for the page. then use Response.Write in chunks,
or if its an actual file, the builtin Reponse.WriteFile
(which just read and writes chunks via .Write).


-- bruce (sqlwork.com)
 
Yeah, sorry I misread you.

...

I think the incremental Reponse.Write is what you're looking for, suggested
via another post.





Phil Johnson said:
Hi sloan, thanks for the sample.

I think I am trying to do something different though, correct me if Im
wrong
though...

Your sample looks like it is a client and streams a file from a url to a
local file.

What I am trying to do is to create a server that has a file stored
locally
and streams that file to the client.
 
I use a "double guid" solution here sometimes.

C:\stuff\

http://www.mycompany.com/stuff/


string folderName = System.Guid.NewGuid().ToString("N");
string fileName = System.Guid.NewGuid().ToString("N");


Which gives me something like this (if I'm writing out a txt file)


C:\stuff\\D680EB2590A542F7828A7F33CF705563\2DB610825821412F8C5CCFA3C642FEE2.txt
or
http://www.mycompany.com/stuff/D680EB2590A542F7828A7F33CF705563\2DB610825821412F8C5CCFA3C642FEE2.txt

Aka, a po' man's security model.
Its ugly, but if somebody guesses 2 guid's..... they totally rock.

...

That is if the information is sensitive.

Naturally, I have to clean up:
C:\stuff\

periodically.




Phil Johnson said:
Thanks for your thoughts Scott...

It looks like buffering the filestream in chunks is going to be the best
option... the files will be temporary files and I need to delete them as
soon
as the data has been streamed.
 
Thanks for all the responses, they certianly got me pointed in the right
direction.... after doing a bit of looking online for the response.write in
chunks method I found this great example from Microsoft.... only requires
changing the string for the filename/path.....

http://support.microsoft.com/?kbid=812406
 
Back
Top