所以我运行了一个httpd服务器,它有一堆文件的链接。假设用户从文件列表中选择三个文件进行下载,它们位于:
mysite.com/file1
mysite.com/file2
mysite.com/file3
当他们点击下载按钮时,我希望他们从上面的链接下载这三个文件。
我的下载按钮类似于:
var downloadButton = new Ext.Button({
text: "Download",
handler: function(){
//download the three files here
}
});
答案 0 :(得分:15)
最好的方法是将文件压缩并链接到:
可以在此处找到其他解决方案:How to make a link open multiple pages when clicked
其中陈述如下:
HTML:
<a href="#" class="yourlink">Download</a>
JS:
$('a.yourlink').click(function(e) {
e.preventDefault();
window.open('mysite.com/file1');
window.open('mysite.com/file2');
window.open('mysite.com/file3');
});
说完这个之后,我仍然会使用压缩文件,因为这个实现需要JavaScript,有时也会被弹出窗口阻止。
答案 1 :(得分:14)
这种方法最适合我,并没有打开新标签,只是下载了我需要的文件/图像:
var filesForDownload = [];
filesForDownload( { path: "/path/file1.txt", name: "file1.txt" } );
filesForDownload( { path: "/path/file2.jpg", name: "file2.jpg" } );
filesForDownload( { path: "/path/file3.png", name: "file3.png" } );
filesForDownload( { path: "/path/file4.txt", name: "file4.txt" } );
$jq('input.downloadAll').click( function( e )
{
e.preventDefault();
var temporaryDownloadLink = document.createElement("a");
temporaryDownloadLink.style.display = 'none';
document.body.appendChild( temporaryDownloadLink );
for( var n = 0; n < filesForDownload.length; n++ )
{
var download = filesForDownload[n];
temporaryDownloadLink.setAttribute( 'href', download.path );
temporaryDownloadLink.setAttribute( 'download', download.name );
temporaryDownloadLink.click();
}
document.body.removeChild( temporaryDownloadLink );
} );
答案 2 :(得分:2)
你可以:
注意 - 选项一客观上更好。
修改强> 找到了一个选项三:https://stackoverflow.com/a/9425731/1803682
答案 3 :(得分:1)
<!DOCTYPE html>
<html ng-app='app'>
<head>
<title>
</title>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="style.css">
</head>
<body ng-cloack>
<div class="container" ng-controller='FirstCtrl'>
<table class="table table-bordered table-downloads">
<thead>
<tr>
<th>Select</th>
<th>File name</th>
<th>Downloads</th>
</tr>
</thead>
<tbody>
<tr ng-repeat = 'tableData in tableDatas'>
<td>
<div class="checkbox">
<input type="checkbox" name="{{tableData.name}}" id="{{tableData.name}}" value="{{tableData.name}}" ng-model= 'tableData.checked' ng-change="selected()">
</div>
</td>
<td>{{tableData.fileName}}</td>
<td>
<a target="_self" id="download-{{tableData.name}}" ng-href="{{tableData.filePath}}" class="btn btn-success pull-right downloadable" download>download</a>
</td>
</tr>
</tbody>
</table>
<a class="btn btn-success pull-right" ng-click='downloadAll()'>download selected</a>
<p>{{selectedone}}</p>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="script.js"></script>
</body>
</html>
app.js
var app = angular.module('app', []);
app.controller('FirstCtrl', ['$scope','$http', '$filter', function($scope, $http, $filter){
$scope.tableDatas = [
{name: 'value1', fileName:'file1', filePath: 'data/file1.txt', selected: true},
{name: 'value2', fileName:'file2', filePath: 'data/file2.txt', selected: true},
{name: 'value3', fileName:'file3', filePath: 'data/file3.txt', selected: false},
{name: 'value4', fileName:'file4', filePath: 'data/file4.txt', selected: true},
{name: 'value5', fileName:'file5', filePath: 'data/file5.txt', selected: true},
{name: 'value6', fileName:'file6', filePath: 'data/file6.txt', selected: false},
];
$scope.application = [];
$scope.selected = function() {
$scope.application = $filter('filter')($scope.tableDatas, {
checked: true
});
}
$scope.downloadAll = function(){
$scope.selectedone = [];
angular.forEach($scope.application,function(val){
$scope.selectedone.push(val.name);
$scope.id = val.name;
angular.element('#'+val.name).closest('tr').find('.downloadable')[0].click();
});
}
}]);
plunker示例:https://plnkr.co/edit/XynXRS7c742JPfCA3IpE?p=preview
答案 4 :(得分:1)
我通过使用window.location以不同的方式解决了这个问题。它适用于Chrome,幸运的是它是我唯一支持的浏览器。可能对某人有用。我最初使用了Dan的答案,这也需要我在这里使用的超时时间,或者只下载了一个文件。
var linkArray = [];
linkArray.push("http://example.com/downloadablefile1");
linkArray.push("http://example.com/downloadablefile2");
linkArray.push("http://example.com/downloadablefile3");
function (linkArray) {
for (var i = 0; i < linkArray.length; i++) {
setTimeout(function (path) { window.location = path; }, 200 + i * 200, linkArray[i]);
}
};
答案 5 :(得分:1)
我喜欢在click()
内的a
元素上执行for loop
事件以进行多个文件下载仅适用于有限数量的文件(在我的情况下为10个文件)。解释这种行为对我有意义的唯一原因是click()
事件执行的下载速度/间隔。
我发现,如果我降低click()
事件的执行速度,那么我将能够下载所有文件。
这是对我有用的解决方案。
var urls = [
'http://example.com/file1',
'http://example.com/file2',
'http://example.com/file3'
]
var interval = setInterval(download, 300, urls);
function download(urls) {
var url = urls.pop();
var a = document.createElement("a");
a.setAttribute('href', url);
a.setAttribute('download', '');
a.setAttribute('target', '_blank');
a.click();
if (urls.length == 0) {
clearInterval(interval);
}
}
我每300毫秒执行一次下载事件click()
。当没有更多文件要下载urls.length == 0
时,我在clearInterval
函数上执行interval
以停止下载。
答案 6 :(得分:0)
您可以执行此操作,创建鼠标事件并将其分派到按钮。 Sourse.
hrefList=['mysite.com/1.jpg', 'mysite.com/2.mp4', 'mysite.com/3.gif'];
buttonDownloadAll=document.createElement('a');
buttonDownloadAll.innerText='Download all';
buttonDownloadAll.href='';
buttonDownloadAll.download=false;
downloadFunc=function(){
buttonDownloadAll.setAttribute('onclick', '');
buttonDownloadAll.download=true;
for(var i=0; i<hrefList.length-1; i++){
buttonDownloadAll.href=hrefList[i];
var clickEvent = document.createEvent('MouseEvent');
clickEvent.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
buttonDownloadAll.dispatchEvent(clickEvent);
}
buttonDownloadAll.setAttribute('onclick', 'downloadFunc()');
buttonDownloadAll.download=false;
buttonDownloadAll.href='';
};
buttonDownloadAll.setAttribute('onclick', 'downloadFunc()');
document.body.appendChild(buttonDownloadAll);
&#13;
答案 7 :(得分:0)
这在所有浏览器(IE11,Firefox,Microsoft Edge,Chrome和Chrome Mobile)中都适用。我的文档位于多个选择元素中。当您尝试过快地执行浏览器时,似乎出现了问题。所以我使用了超时。
makeTexture
此解决方案使用了Promise:
<select class="document">
<option val="word.docx">some word document</option>
</select>
//user clicks a download button to download all selected documents
$('#downloadDocumentsButton').click(function () {
var interval = 1000;
//select elements have class name of "document"
$('.document').each(function (index, element) {
var doc = $(element).val();
if (doc) {
setTimeout(function () {
window.location = doc;
}, interval * (index + 1));
}
});
});
答案 8 :(得分:0)
找到了最简单的方法。甚至可以与div一起使用!
<div onclick="downloadFiles()">
<!--do not put any text in <a>, it should be invisible-->
<a href="path/file1" id="a1" download></a>
<a href="path/file2" id="a2" download></a>
<a href="path/file3" id="a3" download></a>
<script>
function downloadFiles() {
document.getElementById("a1").click();
document.getElementById("a2").click();
document.getElementById("a3").click();
}
</script>
Download
</div>
仅此而已,希望对您有所帮助。
答案 9 :(得分:-1)
这是我发现下载多个文件的最简单方法。
$('body').on('click','.download_btn',function(){
downloadFiles([
['File1.pdf', 'File1-link-here'],
['File2.pdf', 'File2-link-here'],
['File3.pdf', 'File3-link-here'],
['File4.pdf', 'File4-link-here']
]);
})
function downloadFiles(files){
if(files.length == 0){
return;
}
file = files.pop();
var Link = $('body').append('<a href="'+file[1]+'" download="file[0]"></a>');
Link[0].click();
Link.remove();
downloadFiles(files);
}
这应该适合你。 谢谢。
答案 10 :(得分:-2)
//It is possible when using Tampermonkey (Firefox or Chrome).
//They added the GM_Download command.
//You can use it like this download multiple files One time:
// ==UserScript==
// @name
// @description
// @match
// @grant
// @grant GM_download
function setup_reader(file) {
var name = file.name;
var reader = new FileReader();
reader.onload = function (e) {
var bin = e.target.result; //get file content
var lines = bin.split('\n');
for (var line = 0; line < lines.length; line++) {
console.log(lines[line]);
GM_download(lines[line], line + '.jpg');
}
}
// reader.readAsBinaryString(file);
reader.readAsText(file, 'utf-8');//
}