Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BinaryWrite vs WriteFile

Tags:

asp.net

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

like image 772
mhenrixon Avatar asked Apr 17 '09 22:04

mhenrixon


2 Answers

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."

like image 134
ChrisPelatari Avatar answered Oct 18 '22 17:10

ChrisPelatari


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.

like image 30
Keltex Avatar answered Oct 18 '22 17:10

Keltex