Javascript download multiple files

CefiroZ

Limp Gawd
Joined
Jan 17, 2004
Messages
218
I have a table that has a list of files and each row/file has a checkbox associated with it. A user can check these boxes and then click a download button and the idea would be to have each file be fired up as a separate download.

The way I have this set up is a form wraps around the table, and then there's a final input that calls downloadChecked when clicked. I have copied the function below. My problem is that this setup will only download the *last* checked item as it loops through, not *every* checked item. I have already verified the code gets inside the if statement when it should. How can I modify this to get the desired effect, or is there a different/better way to do this?
Code:
function downloadChecked( )
{
    for( i = 0 ; i < document.downloadform.elements.length ; i++ )
    {
	  foo = document.downloadform.elements[ i ] ;
	  if( foo.type == "checkbox" && foo.checked == true )
	  {
		document.location.href='somefile.do?command=download&fileid=' + foo.name ;
	  }
    }
}
 
set up an array of hidden layers/frames/iframes and use those for the download. will take some research but should be doable.
 
The problem with this is that it will replace the current file (webpage) the first time it gets to a checkbox that is true... jiminator is right, send it to a hidden iframe for processing so that the script can continue to the next checkbox.
 
The problem with this is that it will replace the current file (webpage) the first time it gets to a checkbox that is true... jiminator is right, send it to a hidden iframe for processing so that the script can continue to the next checkbox.

Yes, jiminator is correct, I went ahead and implemented that and it works. In case anyone wants it here's the code:
Code:
function makeFrame( url ) 
{ 
    ifrm = document.createElement( "IFRAME" ); 
    ifrm.setAttribute( "style", "display:none;" ) ;
    ifrm.setAttribute( "src", url ) ; 
    ifrm.style.width = 0+"px"; 
    ifrm.style.height = 0+"px"; 
    document.body.appendChild( ifrm ) ; 
}  

function downloadChecked( )
{
    for( i = 0 ; i < document.downloadform.elements.length ; i++ )
    {
	  foo = document.downloadform.elements[ i ] ;
	  if( foo.type == "checkbox" && foo.checked == true )
	  {
		makeFrame('somefile.do?command=download&fileid=' + foo.name );
	  }
    }
}
 
excellent. glad to have helped. actually your iframe logic is better than some we use in our apps. will have to remember that.... :)
 
"quick, gank the code before he takes it down!" hahaha, j/k OP nice job figuring a solution off of Jiminators advice! :)
 
Yes, jiminator is correct, I went ahead and implemented that and it works. In case anyone wants it here's the code:
Code:
function makeFrame( url ) 
{ 
    ifrm = document.createElement( "IFRAME" ); 
    ifrm.setAttribute( "style", "display:none;" ) ;
    ifrm.setAttribute( "src", url ) ; 
    ifrm.style.width = 0+"px"; 
    ifrm.style.height = 0+"px"; 
    document.body.appendChild( ifrm ) ; 
}  

function downloadChecked( )
{
    for( i = 0 ; i < document.downloadform.elements.length ; i++ )
    {
	  foo = document.downloadform.elements[ i ] ;
	  if( foo.type == "checkbox" && foo.checked == true )
	  {
		makeFrame('somefile.do?command=download&fileid=' + foo.name );
	  }
    }
}
quoted for posterity, just in case :p
 
Back
Top