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
|
function uploadFile (url, blob, callback) {
var perFileSize = 2097152; // 2 * 1024 * 1024
var blobParts = Math.ceil(blob.size / perFileSize);
for (var i = 0; i < blobParts; i++) {
(function (i) {
var fd = new FormData();
var _blob = blob.slice(i * perFileSize, (i + 1) * perFileSize);
fd.append('_blob', _blob);
fd.append('filename', blob.name);
fd.append('index', i + 1);
fd.append('total', blobParts);
$.ajax({
type: 'POST',
url: url,
data: fd,
processData: false,
contentType: false
}).done(function (res) {
console.log('upload status: ');
console.log('this is ' + (i + 1) + 'part, total ' + blobParts + ' part(s).');
if (res.statusCode === 200) {
callback(null, res);
}
}).fail(function (err) {
callback(err, null);
});
})(i)
}
}
$('#upload').on('change', function (e) {
var file = e.target.files[0];
uploadFile('/upload', file, function (err, res) {
if (err) { return console.log(err); }
console.log('upload successfully!');
});
});
|