天天看點

備忘錄隻檔案下載下傳功能

down_file:function(files){

var DownFile = is_ie ? downloadFile : IEdownloadFile;

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

(function ($){

var is_ie = !/Trident|MSIE/.test(navigator.userAgent);

function parseURL(str){

return str.lastIndexOf(“/”) > -1 ? str.slice(str.lastIndexOf(“/”) + 1) : str;

};

function downloadFile(fileName, contentOrPath){

var aLink = document.createElement(“a”),

evt = document.createEvent(“HTMLEvents”),

isData = contentOrPath.slice(0, 10) ==”“,

isPath = contentOrPath.lastIndexOf(“.”) > -1;

evt.initEvent(“click”);

aLink.download = fileName;

aLink.href = isPath || isData ? contentOrPath:URL.createObjectURL(new Blob([contentOrPath]));

aLink.dispatchEvent(evt);

};

function IEdownloadFile(fileName, contentOrPath, bool){

var isData = contentOrPath.slice(0, 30) ==”_mainList”,

ifr = document.createElement(‘iframe’);

ifr.style.display = ‘none’;

ifr.src = contentOrPath;

document.body.appendChild(ifr);

isData && ifr.contentWindow.document.write(“”);

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);

}

};

);