js實現單檔案以及多檔案下載

來源:互聯網
上載者:User

標籤:des   style   http   io   ar   使用   java   sp   for   

<script type="text/javascript">
/**
* Javascript 多檔案下載
* @author Barret Lee
* @email [email protected]
*/
var Downer = (function(files){
var h5Down = !/Trident|MSIE/.test(navigator.userAgent);

/**
* 在支援 download 屬性的情況下使用該方法進行單個檔案下載
* @param {String} fileName
* @param {String|FileObject} contentOrPath
* @return {Null}
*/
function downloadFile(fileName, contentOrPath){
var aLink = document.createElement("a"),
evt = document.createEvent("HTMLEvents"),
isData = contentOrPath.slice(0, 5) === "data:",
isPath = contentOrPath.lastIndexOf(".") > -1;

// 初始化點擊事件
evt.initEvent("click",false,false);

// 添加檔案下載名
aLink.download = fileName;

// 如果是 path 或者 dataURL 直接賦值
// 如果是 file 或者其他內容,使用 Blob 轉換
aLink.href = (isPath || isData) ? contentOrPath
: URL.createObjectURL(new Blob([contentOrPath]));

aLink.dispatchEvent(evt);
}

/**
* [IEdownloadFile description]
* @param {String} fileName
* @param {String|FileObject} contentOrPath
*/
function IEdownloadFile(fileName, contentOrPath, bool){
var isImg = contentOrPath.slice(0, 10) === "data:image",
ifr = document.createElement(‘iframe‘);

ifr.style.display = ‘none‘;
ifr.src = contentOrPath;

document.body.appendChild(ifr);

// dataURL 的情況
isImg && ifr.contentWindow.document.write("<img src=‘" +
contentOrPath + "‘ />");

// 儲存頁面 -> 儲存檔案
// alert(ifr.contentWindow.document.body.innerHTML)
if(bool){
ifr.contentWindow.document.execCommand(‘SaveAs‘, false, fileName);
document.body.removeChild(ifr);
} else {
setTimeout(function(){
ifr.contentWindow.document.execCommand(‘SaveAs‘, false, fileName);
document.body.removeChild(ifr);
}, 0);
}
}

/**
* [parseURL description]
* @param {String} str [description]
* @return {String} [description]
*/
function parseURL(str){
return str.lastIndexOf("/") > -1 ? str.slice(str.lastIndexOf("/") + 1) : str;
}

return function(files){
// 選擇下載函數
var downer = h5Down ? downloadFile : IEdownloadFile;

// 判斷類型,處理下載檔案名稱
if(files instanceof Array) {
for(var i = 0, l = files.length; i < l ; i++)
// bug 處理
downer(parseURL(files[i]), files[i], true);
} else if(typeof files === "string") {
downer(parseURL(files), files);
} else {
// 對象
for(var file in files) downer(file, files[file]);
}
}

})();


function down1(){
Downer("http://115.28.175.148/test/files/12_12.xml");
}

function down2(){
Downer(["../file/test.txt","../file/test.txt"]);
}
</script>

<a href="javascript:" onclick="down1(); return false;">單檔案下載</a>

 

<a href="javascript:" onclick="down2(); return false;">多檔案下載</a>

js實現單檔案以及多檔案下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.