js获取文件上传进度
2018-08-06 09:09:27来源:博客园 阅读 ()
js获取文件上传进度:
<input name="file" id="FileUpload" type="file" /> <input type="button" onclick="Submit()" value="提交" />
js监听:
var xhrOnProgress=function(fun) {
xhrOnProgress.onprogress = fun; //绑定监听
//使用闭包实现监听绑
return function() {
//通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
var xhr = $.ajaxSettings.xhr();
//判断监听函数是否为函数
if (typeof xhrOnProgress.onprogress !== 'function')
return xhr;
//如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
if (xhrOnProgress.onprogress && xhr.upload) {
xhr.upload.onprogress = xhrOnProgress.onprogress;
}
return xhr;
}
}
ajax文件上传函数:
function Submit(){
var fileObj = document.getElementById("FileUpload").files[0]; // js 获取文件对象
var formFile = new FormData();
formFile.append("file", fileObj); //加入文件对象
var data = formFile;
$.ajax({
url: url,
data: data,
type: "Post",
dataType: "json",
cache: false,//上传文件无需缓存
processData: false,//用于对data参数进行序列化处理 这里必须false
contentType: false, //必须
xhr:xhrOnProgress(function(e){
var percent=e.loaded/e.total;//文件上传百分比
console.log(percent);
}),
success: function (result) {
console.log(result);
},
})
}
完整代码:
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<input name="file" id="FileUpload" type="file" />
<input type="button" onclick="Submit()" value="提交" />
</body>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
var xhrOnProgress=function(fun) {
xhrOnProgress.onprogress = fun; //绑定监听
//使用闭包实现监听绑
return function() {
//通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
var xhr = $.ajaxSettings.xhr();
//判断监听函数是否为函数
if (typeof xhrOnProgress.onprogress !== 'function')
return xhr;
//如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
if (xhrOnProgress.onprogress && xhr.upload) {
xhr.upload.onprogress = xhrOnProgress.onprogress;
}
return xhr;
}
}
function Submit(){
var fileObj = document.getElementById("FileUpload").files[0]; // js 获取文件对象
var formFile = new FormData();
formFile.append("file", fileObj); //加入文件对象
var data = formFile;
$.ajax({
url: "http://up.qiniu.com/",
data: data,
type: "Post",
dataType: "json",
cache: false,//上传文件无需缓存
processData: false,//用于对data参数进行序列化处理 这里必须false
contentType: false, //必须
xhr:xhrOnProgress(function(e){
var percent=e.loaded/e.total;
console.log(percent);
}),
success: function (result) {
console.log(result);
},
})
}
</script>
</html>
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:css实现导航切换
- 循序渐进VUE+Element 前端应用开发(4)--- 获取后端数据及产 2020-05-29
- 解决 vs code 打开文件总是只有一个tab标签页,新打开的tab 2020-04-23
- 如何配置Tomcat上web.xml让浏览器能直接下载txt,xml类型文 2020-04-14
- 2.reset.css文件 2020-04-08
- javascript中获取dom元素的高度和宽度【转】 2020-03-05
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
