在JavaScript中,可以使用以下方法批量下載文件到本地:
function downloadFile(url, fileName) {
var link = document.createElement('a');
link.href = url;
link.download = fileName;
link.click();
}
function downloadFile(url, fileName) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'blob';
xhr.onload = function() {
if (xhr.status === 200) {
var blob = xhr.response;
var link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = fileName;
link.click();
}
};
xhr.send();
}
function downloadFile(url, fileName) {
fetch(url)
.then(function(response) {
return response.blob();
})
.then(function(blob) {
var link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = fileName;
link.click();
});
}
以上方法都可以實現批量下載文件到本地,只需傳入文件的URL和文件名即可。