jQuery上传多张图片带进度条样式(DEMO)
下面一段代码给大家分享jquery上传多种图片带进度条样式,具体代码如下所示:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>xhr2</title>
</head>
<body>
<div style="text-align: center; margin: 100px">
<input type="file" id="file" name="file" multiple="multiple">
<progress id="uploadprogress" min="0" max="100" value="0">0</progress>
<button onclick="xhr2()">OK</button>
</div>
<script>
function xhr2() {
var xhr = new XMLHttpRequest();//第一步
//定义表单变量
var file = document.getElementById('file').files;
//console.log(file.length);
//新建一个FormData对象
var formData = new FormData(); //++++++++++
formData.append("enctype","multipart/form-data");
//追加文件数据
for (i = 0; i < file.length; i++) {
formData.append("file[" + i + "]", file[i]); //++++++++++
}
//formData.append("file", file[0]); //++++++++++
//post方式
xhr.open('POST', '/common/doUpload'); //第二步骤
xhr.upload.onprogress = function(event) {
if (event.lengthComputable) {
var complete = (event.loaded / event.total * 100 | 0);
var progress = document.getElementById('uploadprogress');
progress.value = progress.innerHTML = complete;
}
};
//发送请求
xhr.send(formData); //第三步骤
//ajax返回
xhr.onreadystatechange = function() { //第四步
if (xhr.readyState == 4 && xhr.status == 200) {
console.log(xhr.responseText);
}
};
//设置超时时间
xhr.timeout = 100000;
xhr.ontimeout = function(event) {
alert('请求超时!');
}
}
</script>
</body>
</html> 相关推荐
qsdnet我想学编程 2020-06-09
huzijia 2020-06-09
pythonclass 2020-06-07
李永毅 2020-06-03
风萧萧梦潇 2020-05-12
cuiwenjie 2020-04-18
码墨 2020-02-14
chouliqingke 2020-02-13
在新项目中使用的是springboot编写的api,涉及到ajax跨域请求和传输文件的问题,在这里记录一下。<input type="button" onclick="test();" value="
daicj 2019-12-25
nxcjh 2020-01-03
fanhuasijin 2019-12-15
zhouhaihua00 2019-12-15
小仙儿 2019-12-01
maidou0 2019-10-19
数据中心运维管理 2019-11-17
81493369 2019-11-04
李永毅 2019-10-29
mmywcoco 2019-10-28
CoderEight 2019-07-02