tags:

views:

425

answers:

2

What should I use for writing a file to the response? There are two different options as I see it. Option one is to read the file into a stream and then write the bytes to the browser with

Response.BinaryWrite(new bytes[5])

Next option is to just write the file from the file system directly with Response.WriteFile. Any advantages/disadvantages with either approach?

Edit: Corrected typos

+2  A: 

The other consideration is whether this is a file which is written one time or frequently. If you are frequently writing this file, then you might want to cache it, thus Response.BinaryWrite makes the most sense.

If you have it in memory, I would not write it to the file system and use Response.WriteFile.

Keltex
Yeah, some of the files are downloaded once a minute. Performance is pretty good still but caching is of course one of the reasons!
mhenrixon
A: 

Response.TransmitFile is preferred if you have the file on disk and are using at least asp.net 2.0.

Response.WriteFile reads the whole file into memory then writes the file to response. TransmitFile "Writes the specified file directly to an HTTP response output stream without buffering it in memory."

blue_fenix
Thanks, I did actually not know about Transfer file. I mostly had my files in database before!
mhenrixon