<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>上传</title>
<link rel="stylesheet" href="/static/js/frame/weui/weui.min.css">
<link rel="stylesheet" href="/static/js/frame/weui/jquery-weui.css">
<style>
.weui_btn_area{
position: relative;
}
input[type=file]{
position: absolute;
left:0;
top:0;
width: 100%;
height:100%;
opacity: 0;
}
</style>
</head>
<body>
<form id="form1">
<div class="weui_progress">
<div class="weui_progress_bar">
<div class="weui_progress_inner_bar js_progress" style="width: 0%;"></div>
</div>
</div>
<div class="weui_btn_area">
<a href="javascript:;" class="weui_btn weui_btn_primary" id="btnStartProgress">上传</a>
<input type="file" name="fileToUpload" id="fileToUpload" multiple="multiple" onchange="uploadFile();"/>
</div>
<div class="weui_btn_area">
<a href="javascript:;" class="weui_btn weui_btn_primary" onclick="cancelFile()" id="cancel">取消</a>
</div>
</form>
<script src="http://cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script type="text/javascript">
var xhr;
function uploadFile() {
var fd = new FormData();
console.log(document.getElementById('fileToUpload').files);
fd.append("fileToUpload", document.getElementById('fileToUpload').files[0]);
xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.addEventListener("load", uploadComplete, false);
xhr.addEventListener("error", uploadFailed, false);
xhr.addEventListener("abort", uploadCanceled, false);
xhr.open("POST", "/index/file"); //修改成自己的接口
xhr.send(fd);
}
function cancelFile() {
xhr.abort();
$('.js_progress').css('width','0');
}
function uploadProgress(e) {
if (e.lengthComputable) {
var percentComplete = Math.round(e.loaded * 100 / e.total);
$('.js_progress').css('width',percentComplete.toString() + '%');
} else {
return false
}
}
function uploadComplete(e) {
console.log(e);
alert('上传成功');
}
function uploadFailed(e) {
console.log(e);
alert("上传失败");
}
function uploadCanceled(e) {
console.log(e);
alert("上传取消");
}
</script>
</body>
</html>
网友评论