一般來說瀏覽器要同時(shí)下載幾個(gè)文件,比如pdf文件,會(huì)在服務(wù)器端把幾個(gè)文件壓縮成一個(gè)文件。但是導(dǎo)致的問題就是會(huì)消耗服務(wù)器的cpu和io資源。
那有沒有辦法,用戶點(diǎn)了幾個(gè)文件,在客戶端同時(shí)下載呢? 支持html5的瀏覽器是可以的,html的a標(biāo)簽有一個(gè)屬性download
a download="下載的1.pdf" href="1.pdf" rel="external nofollow" rel="external nofollow" >單個(gè)文件下載/a>, 經(jīng)過測(cè)試在edge瀏覽器,firefox和chrome都支持。但是遺憾的是ie瀏覽器不支持。參考下面的例子。
!DOCTYPE html>
html>
head>
meta charset="utf-8">
meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
title>/title>
script src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js">/script>
/head>
body>
input type="button" class="downloadAll"
value="批量下載" />
script>
var filesForDownload = [];
filesForDownload[filesForDownload.length] = {
path: "1.zip", //要下載的文件路徑
name: "file1.txt" //下載后要顯示的名稱
};
filesForDownload[filesForDownload.length] = {
path: "2.zip",
name: "file2.txt"
};
filesForDownload[filesForDownload.length] = {
path: "3.zip",
name: "file3.txt"
};
function download(obj) {
var temporaryDownloadLink =
document.createElement("a");
temporaryDownloadLink.style.display =
'none';
document.body.appendChild(
temporaryDownloadLink);
temporaryDownloadLink.setAttribute(
'href', obj.path);
temporaryDownloadLink.setAttribute(
'download', obj.name);
temporaryDownloadLink.click();
document.body.removeChild(
temporaryDownloadLink);
}
$('input.downloadAll').click(function(
e) {
e.preventDefault();
for (var x in filesForDownload) {
download(filesForDownload[x]);
}
});
/script>
/body>
/html>
ie瀏覽器怎么辦呢? 也可以用window.open函數(shù)。
!DOCTYPE html>
html>
head>
meta charset="utf-8">
title>/title>
script src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js">/script>
/head>
body>
a download="下載的1.pdf" href="1.pdf" rel="external nofollow" rel="external nofollow" >單個(gè)文件下載/a>
br>
a href="#" rel="external nofollow" class="yourlink">下載全部文件/a>
script>
$('a.yourlink').click(function(e) {
e.preventDefault();
window.open('1.zip', 'download');
window.open('2.zip', 'download');
window.open('3.zip', 'download');
});
/script>
/body>
/html>
完整的方案就是根據(jù)瀏覽器類型,調(diào)用不同的函數(shù),實(shí)現(xiàn)。
另外要下載pdf,而不是在瀏覽器中打開的話,需要配置apache的配置文件,在httpd.conf中增加下面的配置。
FilesMatch "\.pdf$">
Header set Content-Disposition attachment
/FilesMatch>
或者使用down2組件,下載更簡(jiǎn)單。
JavaScript:
引入頭
head>
metahttp-equiv="Content-Type" content="text/html; charset=utf-8"
/>
title>donw2-多文件演示頁面/title>
linktype="text/css" href="js/down.css" rel="external nofollow" rel="Stylesheet"
/>
scripttype="text/javascript" src="js/jquery-1.4.min.js">
/script>
scripttype="text/javascript" src="js/down.app.js"
charset="utf-8">
/script>
scripttype="text/javascript" src="js/down.edge.js"
charset="utf-8">
/script>
scripttype="text/javascript" src="js/down.file.js"
charset="utf-8">
/script>
scripttype="text/javascript" src="js/down.folder.js"
charset="utf-8">
/script>
scripttype="text/javascript" src="js/down.js"
charset="utf-8">
/script>
/head>
創(chuàng)建down2對(duì)象
var downer = new DownloaderMgr();
downer.Config["Folder"] = ""; //設(shè)置默認(rèn)下載路徑。
//掛載事件
downer.event.taskCreate = function(obj) {
$(document.body).append("文件ID:" + obj
.fileSvr.id) + "br/>";
};
downer.event.downProcess = function(obj) {};
downer.event.downStoped = function(obj) {};
downer.event.downComplete = function(
obj) {
$(document.body).append('div>本地路徑:' +
obj.fileSvr.pathLoc + '/div>');
};
downer.event.downError = function(obj,
err) {};
downer.event.queueComplete = function() {
$(document.body).append(
"div>隊(duì)列完成/div>");
};
批量下載url
$("#btn-down-files").click(function() {
if (downer.Config["Folder"] == "") {
downer.open_folder();
return;
}
var urls = [
{
fileUrl: "http://res2.ncmem.com/res/images/ie11.png"
}
, {
fileUrl: "http://res2.ncmem.com/res/images/up6.1/down.png"
}
, {
fileUrl: "http://res2.ncmem.com/res/images/firefox.png"
}
, {
fileUrl: "http://res2.ncmem.com/res/images/edge.png"
}
, {
fileUrl: "http://res2.ncmem.com/res/images/up6.1/cloud.png"
}
, {
fileUrl: "http://res2.ncmem.com/res/images/home/w.png"
}
, {
fileUrl: "http://res2.ncmem.com/res/images/img.png"
}
];
downer.app.addUrls(urls);
});
當(dāng)成一個(gè)文件夾下載
$("#btn-down-json").click(function() {
if (downer.Config["Folder"] == "") {
downer.open_folder();
return;
}
var fd = {
nameLoc: "圖片列表"
,
files: [
{
fileUrl: "http://res2.ncmem.com/res/images/ie11.png"
}
, {
fileUrl: "http://res2.ncmem.com/res/images/up6.1/down.png"
}
, {
fileUrl: "http://res2.ncmem.com/res/images/firefox.png"
}
, {
fileUrl: "http://res2.ncmem.com/res/images/edge.png"
}
, {
fileUrl: "http://res2.ncmem.com/res/images/up6.1/cloud.png"
}
, {
fileUrl: "http://res2.ncmem.com/res/images/home/w.png"
}
, {
fileUrl: "http://res2.ncmem.com/res/images/img.png"
}
]
};
downer.app.addJson(fd);
});
下載多級(jí)目錄
$("#btn-down-fd").click(function() {
if (downer.Config["Folder"] == "") {
downer.open_folder();
return;
}
var fd = {
nameLoc: "測(cè)試文件夾"
,
files: [
{
fileUrl: "http://www.ncmem.com/images/ico-ftp.jpg"
}
, {
fileUrl: "http://www.ncmem.com/images/ico-up.jpg"
}
]
,
folders: [
{
nameLoc: "圖片1"
,
files: [
{
fileUrl: "http://www.ncmem.com/images/ico-ftp.jpg"
}
, {
fileUrl: "http://www.ncmem.com/images/ico-up.jpg"
}
, {
fileUrl: "http://www.ncmem.com/images/ico-capture.jpg"
}
, {
fileUrl: "http://www.ncmem.com/images/ico-imageuploader.gif"
}
, {
fileUrl: "http://www.ncmem.com/images/ico-wordpaster.gif"
}
]
,
folders: [
{
nameLoc: "軟件"
,
files: [
{
fileUrl: "http://res2.ncmem.com/res/images/edit-file.png"
}
]
}
]
}
]
};
downer.app.addJson(fd);
});
自定義下載文件名稱
$("#btn-down-svr").click(function () {
if (downer.Config["Folder"] == "") { downer.open_folder(); return; }
var urls = [
{ fileUrl: "http://localhost:90/db/down.aspx", nameLoc: "test.exe" }
, { fileUrl: "http://localhost:90/db/down.aspx", nameLoc: "test-1.exe" }
];
downer.app.addUrls(urls);
});
實(shí)現(xiàn)效果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- PHP大文件分割分片上傳實(shí)現(xiàn)代碼
- php實(shí)現(xiàn)斷點(diǎn)續(xù)傳大文件示例代碼
- PHP下載大文件失敗并限制下載速度的實(shí)例代碼
- PHP超低內(nèi)存遍歷目錄文件和讀取超大文件的方法
- 詳解PHP多個(gè)進(jìn)程配合redis的有序集合實(shí)現(xiàn)大文件去重
- PHP如何通過表單直接提交大文件詳解
- PHP大文件分片上傳的實(shí)現(xiàn)方法
- php下載遠(yuǎn)程大文件(獲取遠(yuǎn)程文件大小)的實(shí)例
- 詳解PHP如何讀取大文件