I have interesting task which requires me to download a dynamically generated file from a server (ASP.NET) to the client. On the client side it is just JavaScript (jQuery) and the client is able to generate a lot of parameters to customize how the data is exported.
What is the best way to do download the file from the server? Should I use a WCF service such as what is described here or simple page like this one?
I don’t know how to download a file without reloading the page (I’m not sure that $.ajax will work in this case). Could someone please give me some direction on this topic? Thanks.
First you can create the file from a handler
.ashxLet say that you have the file for downloading at
download.ashxand you have some parametres to pass from your javascript, egdownload.ashx?p1=8827&p2=8831to know what you going to create.Then on your javascript you simple can make a redirect as
or alternative you can use the
window.openfor do the same thinkand your file will start the download.
Just make sure that you have set the header of attachment, and the correct contenttype on your handle eg:
Simple and clear, both tested and working.
Also you may interesting on this answer: How to handle errors.