Monday, February 25, 2008

Save server memory with Response.TransmitFile()

So, two common tasks that I encounter when sending files to the browser:
  1. Open file within the browser
  2. Open "file download" dialog box

I used to use the Response.Write() method, but recently came across the Response.TransmitFile() method. This method directly writes the file to the HTTP response stream and saves you from hogging up memory on the server.

1) Open file within the browser
Response.ContentType = "application/pdf";
//write file to the browser without saving file in server memory
Response.TransmitFile("c:\docs\pdfs\test1_1.pdf");
Response.End();
2) Open "file download" dialog box
Response.ContentType = "application/pdf";
//add 'attachment' header to open 'file download' dialog box
Response.AddHeader("content-disposition", "attachment; filename=test1_1.pdf");
//write file to the browser without saving file in server memory
Response.TransmitFile("c:\docs\pdfs\test1_1.pdf");
Response.End();

Some other types of "write" methods that exist in the System.Web.HttpResponse class are

  • BinaryWrite()- use this method to output a file that is stored in a Base64 string format
    Response.BinaryWrite(Convert.FromBase64String(EncodedDocument));
  • WriteFile()-use this method if you desire for the file to be stored in memory, or if the file is already in memory. This method is expensive because it stores the file object in memory, then transmits to the client.
  • Write() -there are a few overloads for the type of objects that Write() can send to the client. It is a flexible method and TransmitFile() or BinaryWrite() may better fit your specific needs

Thursday, February 14, 2008

Indiana Jones Trailer!!

The fourth installment of Indy! http://gizmodo.com/356366/indiana-jones-iv-trailer-makes-us-wet-our-pants