Jeremy Stein - Brain

« »

Download multiple files

To send multiple files to the web browser, embed the files in hidden iframes like this:

<iframe id="file_download1" width="0" height="0" scrolling="no" frameborder="0" src="file1.zip"/>
<iframe id="file_download2" width="0" height="0" scrolling="no" frameborder="0" src="file2.zip"/>
<iframe id="file_download3" width="0" height="0" scrolling="no" frameborder="0" src="file3.zip"/>

Or here’s a fun little page that demonstrates the technique with Javascript:

<html>
  <head>
    <script language="javascript">
      function download(form) {
	for(var i=0; i<form.elements.length; i++) {
	  var element = form.elements[i];
	  if (element.type == "checkbox") {
	    if (element.checked) {
	      var iframe = document.createElement("iframe");
	      iframe.width = iframe.height = iframe.frameBorder = 0;
	      iframe.scrolling = "no";
	      iframe.src = element.value;
	      document.getElementById("iframe_holder").appendChild(iframe);
	    }
	  }
	}
      }
    </script>
  </head>
  <body>
    <form>
      <input type="checkbox" name="file1" value="file1.zip"/>file1.zip<br/>
      <input type="checkbox" name="file2" value="file2.zip"/>file2.zip<br/>
      <input type="checkbox" name="file3" value="file3.zip"/>file3.zip<br/>
      <input type="button" onclick="download(this.form)" value="Download"/>
    </form>
    <div id="iframe_holder"/>
  </body>
</html>

February 29, 2008 3 Comments.

3 Comments

  1. Kenneth replied:

    I tried the Javascript for Downloading Multiple Files but it does not work?

    Any insight?

    July 26th, 2011 at 3:25 am. Permalink.

  2. Jeremy replied:

    I’m not sure why the JavaScript doesn’t work. But it should give you an idea of the approach you can take. Good luck!

    July 26th, 2011 at 9:08 am. Permalink.

  3. Pradeep replied:

    Super Cool…Though the exact snippet doesn’t work, I was able to plug in the relevant code and it works like a charm! Thanks..

    September 29th, 2011 at 1:09 pm. Permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

Why ask?

« »