forked from qiumanlover/nodejs-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyFileUpload.js
143 lines (132 loc) · 4.1 KB
/
myFileUpload.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var pieceSize = 1024 * 1024 * 2;
function fileSelected() {
document.getElementById('fileInfo').innerHTML = '';
var e = arguments.callee.caller.arguments[0] || event;
var evt = e.srcElement || e.target;
var fileCount = evt.files.length;
for (var i = 0; i < fileCount; i++) {
var file = evt.files[i];
var fileSize = 0;
if (file.size > 1024 * 1024 * 1024) {
fileSize = (Math.round(file.size * 100 / (1024 * 1024 * 1024)) / 100).toString() + 'GB';
} else if (file.size > 1024 * 1024) {
fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB';
} else {
fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';
}
var fileinfo = '<ul><li>Name: ' + file.name + '</li><li>Size: ' + fileSize + '</li><li>Type: ' + file.type + '</li></ul>';
document.getElementById('fileInfo').innerHTML += fileinfo;
}
}
function uploadFileWhole(file) {
var xhr = new XMLHttpRequest();
var fileinfo = {
filename: file.name,
filesize: file.size
};
xhr.open('post', '../Home/SetFileInfo', true);
xhr.responseType = 'json';
xhr.send(JSON.stringify(fileinfo));
var isok = false;
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
console.log(xhr.responseText);
isok = !isok;
if (isok) {
xhr.open("POST", "../Home/MyUpload", true);
xhr.send(file);
}
}
}
}
function uploadFilePieceByBlob(file) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
var index = xhr.response.index;
console.log(index / 1024 / 1024 + ' ' + index);
if (index < file.size) {
var lastindex = index + pieceSize;
lastindex = lastindex < file.size ? lastindex : file.size;
var blob = file.slice(index, lastindex);
xhr.open('post', '../Home/TransferData', true);
xhr.responseType = 'json';
xhr.send(blob);
}
}
}
var fileinfo = {
filename: file.name,
filesize: file.size
};
xhr.open('post', '../Home/SetFileInfo', true);
xhr.responseType = 'json';
xhr.send(JSON.stringify(fileinfo));
console.log(file.size);
}
function uploadFilePieceByFormData(file) {
var pieceCount = Math.ceil(file.size / pieceSize);
var url = 'http://localhost:3000';
//var url = '../Home/UploadByPiece';
var xhr = new XMLHttpRequest();
//xhr.responseType = 'json';
var index = 1;
var formdata = new FormData();
formdata.append("filename", file.name);
formdata.append("pieceCount", pieceCount);
formdata.append("curPiece", index);
formdata.append("data", file.slice((index - 1) * pieceSize, index * pieceSize));
xhr.open("POST", url, true);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
var result = xhr.response;
result = eval('(' + result + ')');
console.log(result.nxtPiece);
index = result.nxtPiece - 1;
index++;
if (index <= pieceCount) {
formdata = new FormData();
formdata.append("filename", file.name);
formdata.append("pieceCount", pieceCount);
formdata.append("curPiece", index);
formdata.append("data", file.slice((index - 1) * pieceSize, index * pieceSize));
xhr.open("POST", url, true);
xhr.send(formdata);
}
}
}
xhr.send(formdata);
}
function nodeUploadWholeByFormData(file) {
var formdata = new FormData();
formdata.append("filename", file.name);
formdata.append("pieceCount", 1);
formdata.append("curPiece", 1);
formdata.append("data", file);
var xhr = new XMLHttpRequest();
xhr.responseType = 'json';
xhr.open("POST", "http://localhost:3000", true);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
console.log(xhr.response.nxtPiece);
}
}
xhr.send(formdata);
}
function nodeUploadWhole(file) {
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://localhost:3000", true);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
console.log(xhr.responseText);
}
}
xhr.send(file);
}
function uploadfiles() {
var files = document.getElementById("fileToUpload").files;
for (var i = 0; i < files.length; i++) {
uploadFilePieceByFormData(files[i]);
}
console.log('upload success.')
}