tags:

views:

33

answers:

3

When a user clicks a button, the server generates an Excel file.

When it is generated, I need for it be sent to the user as a standard download.

Is the correct way to do this to redirect the user to the path of the Excel file?

Thanks

+1  A: 

Do you control the process that creates the file? You could set the content type and stream the result directly to the browser without actually creating a physical copy of the Excel file to save a step.

jaltiere
+1  A: 

Usually what I do is present the user with a link on the page to the file -- usu. a handler that actually generates the file live -- then use javascript to set the page location to the href in the link. Since the file is an attachment, it doesn't redirect the page but instead opens the download dialog. If, for some reason, the download doesn't start -- say, javascript is turned off -- the user can always click the link to download the document.

tvanfosson
+3  A: 

Stream it as an attachment

byte[] bytes = your generated file
Response.ClearHeaders();
Response.Clear();
Response.AddHeader( "Content-Disposition", "attachment; filename=bob.xls" );
Response.ContentType = "application/vnd.ms-excel";
Response.BinaryWrite( bytes );
Response.End();
Sky Sanders