-
Notifications
You must be signed in to change notification settings - Fork 321
/
Copy pathdemo.html
69 lines (64 loc) · 2.5 KB
/
demo.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>异步上传文件demo</title>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script src="/jquery.ui.widget.js"></script>
<script src="/jquery.fileupload.js"></script>
</head>
<body>
<input id="fileupload" type="file" name="file" />
<script>
$(function() {
$('#fileupload').fileupload({
url: '/upload',
dataType: 'json',
autoUpload: false,
add: function(e, data) {
// http://stackoverflow.com/questions/17451629/maxfilesize-and-acceptfiletypes-in-blueimp-file-upload-plugin-do-not-work-why
var uploadErrors = [];
// if (data.files[0] && !/(\.|\/)(gif|jpe?g|png)$/i.test(data.files[0].name)) {
// uploadErrors.push('上传的不是图片');
// }
// if (data.files[0] && data.files[0].size > 5000000) { // 5M
// uploadErrors.push('图片尺寸太大');
// }
if (uploadErrors.length > 0) {
alert(uploadErrors.join("\n"));
} else {
data.submit();
}
},
done: function(e, data) {
data = data.result;
if (data.success === 1) {
console.log('upload success!file path:%s', data.href);
} else {
console.error(data.msg);
}
},
xhr: function() {
// 显示进度。 https://xhr.spec.whatwg.org/#interface-progressevent
var xhr = new window.XMLHttpRequest();
xhr.upload.addEventListener("progress", function(evt) {
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
percentComplete = parseInt(percentComplete * 100);
console.log(percentComplete + '%');
if (percentComplete === 100) {
console.log('加载完成')
}
}
}, false);
return xhr;
},
}).bind('fileuploadsubmit', function(e, data) {
// 如果不这么做,会提交整个表单的数据
var $file = $('#fileupload');
data.formData = $file.serializeArray();
});
});
</script>
</body>
</html>