Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a file download in ASP.NET AJAX

I would like to use standard ASP.NET file download response, like in other Stack Overflow question.

Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition","attachment; filename=logfile.txt");
Response.TransmitFile( Server.MapPath("~/logfile.txt") );
Response.End();

But inside update panel it does not work. What I have to do, that I will get a file if the download event is triggered inside update panel?

like image 593
Peter Stegnar Avatar asked Aug 26 '09 07:08

Peter Stegnar


2 Answers

Well, I have found nice blog post on Encosia which describe solution of this ASP.NET AJAX file download problem. It work really nice.

http://encosia.com/2007/02/23/ajax-file-downloads-and-iframes/

like image 52
Peter Stegnar Avatar answered Oct 30 '22 08:10

Peter Stegnar


You need to have this in a separate aspx that does not use ajax. Ajax is updating existing html markup on a page at the client side. What you try here is replace the responce content on the server side before sending anything to the client.

You could try this:

Have a page called Download.aspx that contains the transmit code you already have.

In your original page, you have a javascript call that calls the download page like this:

window.location.replace('Download.aspx');
like image 24
awe Avatar answered Oct 30 '22 08:10

awe