Javascript使用iframe下载多个文件 [英] Javascript download multiple files with iframe
本文介绍了Javascript使用iframe下载多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要用一个按钮下载多个文件全部下载,我不想压缩它们,我想用javascript和隐藏的iframe下载它们,我怎么能这样做?这里是iframe和javascript的例子,但是我不明白我需要发送什么url到ifrm.setAttribute(src,url);什么是'somefile.do'?
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 );
}
}
}
推荐答案
我建议使用 multiDownload jQuery插件。它也使用iframe,并保持您的代码整洁。
I would recommend the multiDownload jQuery plugin. It's also using iframes, and keeps your code tidy.
示例:
<a href="document1.zip" class="document">document 1</a>
<a href="document2.zip" class="document">document 2</a>
<a href="document3.zip" class="document">document 3</a>
<a href="#" id="download_all">download all</a>
$('#download_all').click(function (event) {
event.preventDefault();
$('.document').multiDownload();
});
你准备好了:)
这篇关于Javascript使用iframe下载多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文