Dowloading multiple PDF files from javascript

前端 未结 1 1586
长发绾君心
长发绾君心 2021-01-16 18:05

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

相关标签:
1条回答
  • 2021-01-16 18:34

    I do the following:

    1. Include the exchanger.js javascript file in your head section
    2. Initialize the exchanger object on page load: theBuffer = new exchanger('dwnld');
    3. 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&param2=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

    4. 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.

    0 讨论(0)
提交回复
热议问题