-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsliceFile.js
More file actions
executable file
·96 lines (93 loc) · 2.97 KB
/
sliceFile.js
File metadata and controls
executable file
·96 lines (93 loc) · 2.97 KB
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
//entry function
function readySlice() {
//get the filename of the URL
var file = document.getElementById('file').files[0];
if (!file) {
Log.e("File", "No file seleted");
alert("No file seleted");
return false;
}
var chunkSize = 1024 * document.getElementById('chunkSize').value;
chunkSize = chunkSize < file.size ? chunkSize : file.size;
var fileName = file.name;
var fileType = fileName.substr(fileName.lastIndexOf('.') + 1);
var fileName = fileName.substring(0, fileName.lastIndexOf('.'));
//judge the type of the media file
Log.i("Upload","Start slice and upload file: "+fileName+"."+fileType+" slice size: "+chunkSize);
if (fileType == "webm" || fileType == "mp4") {
sliceAndSend(file, fileName, fileType, chunkSize);
} else {
Log.e("File", "File type is not supported");
alert("File type is not supported");
}
}
//slice Webm file and send chunk and create json array
function sliceAndSend(file, fileName, fileType, chunkSize) {
var chunkNum = Math.ceil(file.size / chunkSize);
//json
var fileJson = {};
fileJson["fileName"] = fileName + "." + fileType;
fileJson["fileType"] = fileType;
fileJson["fileLength"] = file.size;
fileJson["chunkJsons"] = [];
//send chunk
var i = 0;
(function sendChunk(i) {
var startByte = chunkSize * i;
var chunk = file.slice(startByte, startByte + chunkSize);
chunkSize = chunk.size;
var chunkName = fileName + i + "." + fileType;
//write json
var chunkJson = {};
chunkJson["chunkName"] = "uploads/" + fileName + "/" + chunkName;
chunkJson["chunkOffset"] = startByte;
chunkJson["chunkSize"] = chunkSize;
fileJson["chunkJsons"].push(chunkJson);
//send chunk to server
url = "saveFile.php"; //server url
var xhrSend = new XMLHttpRequest();
xhrSend.open('POST', url, true);
xhrSend.setRequestHeader("X-File-Name", chunkName);
xhrSend.setRequestHeader("X-File-Size", chunk.size);
xhrSend.setRequestHeader("X-Dir-Name", fileName);
//xhr.setRequestHeader("Content-Type","multipart/form-data");
var formdata = new FormData();
formdata.append("file", chunk);
xhrSend.send(formdata);
xhrSend.onload = function() {
if (xhrSend.status != 200) {
alert("Upload","Send chunk" + i + "error");
Log.e("Upload","Send chunk" + i + "error");
}
if (i < chunkNum - 1) {
i++;
sendChunk(i);
} else {
//upload json
var jsonString = JSON.stringify(fileJson);
var jsonName = fileName + ".json";
uploadJson(jsonString, jsonName);
}
}
})(i);
}
function uploadJson(jsonString, jsonName) {
url = "saveJson.php";
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
var formdata = new FormData();
xhr.setRequestHeader("X-File-Name", jsonName);
formdata.append("json", jsonString);
xhr.send(formdata);
xhr.onload = function() {
if (xhr.status != 200) {
alert("Send json-file error");
Log.e("Upload", "Send json-file error")
return false;
} else {
//alert(xhr.response);
Log.i("Upload", "ALL is Successful");
alert("ALL is Successful");
}
}
}