Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

Javascript download multiple files with iframe

I need to download multiple files with one button "download all",I don't want to zip them, i want to download them with javascript and hidden iframe,how I can do it? here is the example with iframe and javascript ,but I don't understand what url I need to send to ifrm.setAttribute( "src", url ) ;What is 'somefile.do'?

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 );
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to pass direct file URL (or URL which cause direct downloading) into iframe src. In your script case, somefile.do is probably some download handler and file ID and action is passed by it's parameters.

But back to your question, if you have files:

  • example.com/file1.zip
  • example.com/file2.zip
  • example.com/file3.zip

You have to pass it just into makeFrame function as a parameter:

makeFrame('http://example.com/file1.zip');

It will create iframe for every URL and start downloading (or display save dialog - depends on browser). Make sure to have files URLs in array and pass them in for loop.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...