I have a gridview in which I have one column for downloading pdf files for each row. I fire a javascript function which uses "window.location.href" to create and download PDF file from another page. Now on Clientclick of some button ,I am calling a javascript function in which a for loop reads each line of gridview and fire click event (of the button which i used in grid to download PDF) for downloading multiple PDF files at once for all the rows. By using this technique I am only getting PDF with the details of last row only ,that is m getting only one PDF after firing click event for each row.
Asked
Active
Viewed 1,771 times
1 Answers
1
I do the following:
- Include the exchanger.js javascript file in your head section
- Initialize the exchanger object on page load:
theBuffer = new exchanger('dwnld');
Create a javascript function that you will call whenever you want to initiate a file download :
function downloadFile(){ // you can add parameters to the function as needed to pass in dynamic data sent to the back end download handler data = "http://your_backend_file_download_handler.php?param1=val1¶m2=val2&etc=whatever"; // send whatever data you need to the php program via query string parameters theBuffer.sendData(data); // initiate the file download }
Note: The php back end file download program that handles the requests can do whatever it needs to do with the parameters you send it in order to put together/retrieve the correct data/file for download. After much tinkering this combination is what consistently works for me
Include this little bit of html in your body section. I usually put it just before the closing body tag:
<iframe name="dwnld" id="dwnld" style="width:0;height:0;border:0"> </iframe> Note: the id value assigned to the iframe is the same value given in step 2 when initializing.
The result is that the user never leaves the current page to download any number of files because the actual download is handled in a separate page (aka the iframe). I have used it without issue in all of my projects for years now.

Drew
- 4,215
- 3
- 26
- 40
-
What should be in my `"http://your_backend_file_download_handler.php`? What should it do? – 1252748 Aug 08 '13 at 18:42
-
@thomas the backend handler would be exactly the php code I placed in my answer to your question here: http://stackoverflow.com/questions/18131797/adding-header-will-not-make-a-file-download/18131908?noredirect=1#comment26554996_18131908 – Drew Aug 08 '13 at 18:45
-
I've enclosed in a jQuery document.ready function a click function attached to my `` that has `new exchanger` and then calls the `downloadfile()` function. I put the iFrame at the bottom as suggested. But when i click the `` the browser still goes to the file. Any ideas why? – 1252748 Aug 08 '13 at 18:57
-
1In your click handler you need to stop the default action of clicking on the href with preventDefault: http://api.jquery.com/event.preventDefault/ – Drew Aug 08 '13 at 19:04
-
Yes, I forgot to mention, I added that as well. – 1252748 Aug 08 '13 at 20:49
-
This is actually how I have it set up. Does is look correct? http://jsfiddle.net/vARwx/ – 1252748 Aug 08 '13 at 21:04
-
Working now. Typo -_- Thanks for the help and the extension! – 1252748 Aug 08 '13 at 21:10
-
1@thomas Great! Can I get an upvote or a selected answer for your question that references this one? Thanks :-) – Drew Aug 09 '13 at 00:49