js实现单文件以及多文件下载
<script type="text/javascript">
/**
* Javascript 多文件下载
* @author Barret Lee
* @email barret.china@gmail.com
*/
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实现单文件以及多文件下载的更多相关文章
- SpringMVC单文件上传、多文件上传、文件列表显示、文件下载(转)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 本文详细讲解了SpringMVC实例单文件上传.多文件上传.文件列表显示.文件下载. 本文工程 ...
- [转]SpringMVC单文件上传、多文件上传、文件列表显示、文件下载
一.新建一个Web工程,导入相关的包 springmvc的包+commons-fileupload.jar+connom-io.jar+commons-logging,jar+jstl.jar+sta ...
- JAVA SpringMVC + FormDate + Vue + file表单 ( 实现 js 单文件和多文件上传 )
JS 部分 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <tit ...
- vue.js单文件组件中非父子组件的传值
最近在研究vue.js,总体来说还算可以,但是在web开发群里有一些人问在单文件组件开发模式中非父子组件如何传值的问题,今天在这里讲讲,希望对大家有所帮助! 在官网api中的这段讲解很少,也很模糊:官 ...
- 开源作品-PHP写的JS和CSS文件压缩利器(单文件绿色版)-SuMinify_PHP_1_5
前言: 网站项目需要引用外部文件以减小加载流量,而且第一次加载外部资源文件后,其他同域名的页面如果引用相同的地址,可以利用浏览器缓存直接读取本地缓存资源文件,而不需要每个页面都下载相同的外部资源文件. ...
- Spring MVC-------文件上传,单文件,多文件,文件下载
Spring MVC 框架的文件上传是基于 commons-fileupload 组件的文件上传,只不过 Spring MVC 框架在原有文件上传组件上做了进一步封装,简化了文件上传的代码实现,取消了 ...
- Vue.js的组件(slot/动态组件等)、单文件组件、递归组件使用
一.组件 1> 组件命名方式有两种(注意:在DOM模板中只有kebab-case命名方法才生效): html中引用组件: <!-- 在DOM模板中,只有 kebab-case命名才生效 - ...
- grunt 单独压缩多个js和css文件【转】
原文地址:http://xiaomiya.iteye.com/blog/2177877 使用grunt来压缩前端js,css文件 因为最近做的客户端本地项目有用到十几个js,js提交之前都需要压缩.用 ...
- node.js表单——formidable/////z
node.js表单--formidable node处理表单请求,需要用到formidable包.安装formidable包的命令如下: npm install formidable 安装pack ...
随机推荐
- Linux下错误的捕获:全局变量errno和strerror()
经常在调用linux 系统api 的时候会出现一些错误,比方说使用open() write() creat()之类的函数有些时候会返回-1,也就是调用失败,这个时候往往需要知道失败的原因.这个时候使用 ...
- as3自定义事件
package EventPackage { import flash.events.Event; /** * * @author tqr <br /> * 创建时间:2015-2-6 下 ...
- include,import,@class的区别
1.#include与#import功能一样,都是导入头文件 2.区别是#include是单纯导入头文件,如果重复导入头文件,头文件就被导入多分 3.#import在导入头文件之前会检查之前是否导入过 ...
- linq 实现group by 不使用group关键字 等同lambad表达式中的group join 查询一对多关系
return from orderInfo in orderEntity.x_s_orderInfo join oState in orderEntity.x_s_oStatuInfo on orde ...
- composer 安装
安装composer wget http://curl.haxx.se/ca/cacert.pem curl -sS https://getcomposer.org/installer | php - ...
- ABP的语言切换
在ABP官网http://www.aspnetboilerplate.com/创建一个Multi Page Web Application项目并打开,在Web项目下可以找到一个Controllers/ ...
- XE3随笔15:从XML中解析
SuperObject 文件包中还有一个 SuperXmlParser 单元, 可以从 XML 中解析出 ISuperObject. SuperXmlParser 只有三个函数: XMLParseSt ...
- windows获取硬盘使用率等信息
#coding=utf8 import psutil cpu = {'user' : 0, 'system' : 0, 'idle' : 0, 'percent' : 0} mem = {'total ...
- dos2unix unix2dos
实现windows和linux之间的文件自动转换,消除^M.
- win10U盘 安装
转载自网络: 首先,现在WIN10镜像文件 1.地址和具体信息如下: 下载提示:请用迅雷等支持P2P的下载工具下载 Win10 TH2正式版微软官方原版ISO系统镜像下载(2016年4月更新版): W ...