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 without saving the file to memory on the server, thus saving server memory (especially for big files).

1) Open file within the browser: The following code will preface the response object as a PDF file, and then transmit the file directly to the HTTP response.  Then, it will flush the response to the browser, and stops execution of the page.  Typically, the behavior that this evokes is Adobe Reader will open inside the browser and download the document so that the user can view its contents within Adobe Reader.

Response.ContentType = "application/pdf";
//write file to the buffer
Response.TransmitFile(@"e:\inet\www\docs\testdoc.pdf");
Response.End();

2) Open "file download" dialog box: If you do not want the PDF to automatically open inside the browser window, you can give the user the "File Download" option.  This dialog box typically lets the user either open, save, or cancel the file.  The method for communicating this behavior to the browser is to add an http header "content-disposition" to the response object. 

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=testdoc.pdf");
Response.TransmitFile(@"e:\inet\www\docs\testdoc.pdf");
Response.End();
 
This is the 'File Download' box that pops up using the second method

 

Published Monday, February 25, 2008 12:21 PM by mamasu

Comments

# re: Save server memory with Response.TransmitFile()

Thursday, July 24, 2008 6:08 AM by Malay

Giving error while opening the file through acrobat reader

# re: Save server memory with Response.TransmitFile()

Saturday, October 11, 2008 6:06 AM by rahulshukla_iitk

I just wish to clarify whether Response.End after Response.TransmitFile will kill the request as I observed RequestQueue Growing when we use TransmitFile instead of WriteFile.

Regards

RS

http://www.healthsprint.com/

# re: Save server memory with Response.TransmitFile()

Friday, October 17, 2008 1:34 AM by hunny

www.improve.dk/.../response-transmitfile-close-will-kill-your-application

please check this link

# re: Save server memory with Response.TransmitFile()

Friday, January 09, 2009 10:19 AM by Leonard Van Der Merwe (Shwaa)

When using Response.Write and Response.Transmitfile in a update panel,

you might need to register the control in the Page_Load:

ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(Button1);

# re: Save server memory with Response.TransmitFile()

Monday, July 27, 2009 5:14 PM by Timmy

Leonard Van Der Merwe (Shwaa) you saved me.

I was using an Update panel.

ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(Button1);

is very important

# re: Save server memory with Response.TransmitFile()

Friday, September 11, 2009 5:44 AM by Kalyan

I am getting some nonsense stuff and some aspx code in the excel file when I download

# re: Save server memory with Response.TransmitFile()

Thursday, September 24, 2009 2:21 AM by vkdwivedi

Can someone give me a full working code for this? I am not able to make this example work.

# re: Save server memory with Response.TransmitFile()

Tuesday, November 10, 2009 10:32 AM by Jesus

Excelent the "ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(Button1);" part.

# re: Save server memory with Response.TransmitFile()

Thursday, January 07, 2010 5:51 AM by vg

I want to try this, can some one provide some more details.

# re: Save server memory with Response.TransmitFile()

Monday, May 09, 2011 3:42 AM by tahazubairahmed

How can i find that the file i download is present in folder.

Leave a Comment

(required) 
(required) 
(optional)
(required)