/ *
* /
var slice_upload = {
fileInput: null, //html file
fileFilter: [], //
url: "", //ajax
nSlice_count: 100, //
nFactCount: null, //
nMin_size: 0.5, //(M)
nMax_size: 5, //(M),
totalSize: 0, //,
uploadSize: 0, //
error: 0,
fdata: "", //,
timeout: 60000, //,
errormsg: "",
filter: function (files) { //
return files;
},
current_upload: 0, //
onFailure: function () {}, //
onDelete: function () {}, //
onSelect: function () {}, //
onPause: function () {}, //
formData: {},
//file
funGetFiles: function (e) {
//
var files = e.target.files || e.dataTransfer.files;
//
files = this.filter(files);
for (var i = 0; i < files.length; iPP) {
this.fileFilter.push(files[i]);
}
this.funDealFiles();
return this;
},
//
funDealFiles: function () {
this.totalSize = 0;
for (var i = 0, file; i < this.fileFilter.length; iPP) {
file = this.fileFilter[i];
//
file.index = i;
this.totalSize += file.size;
file.upSize = 0;
file.content = "";
file.pause = false;//
file.end_upload = false;//
if (file.hasOwnProperty("nCountNum") == false) {
//
file.nCountNum = 0;
file.nFactSize = file.size / this.nSlice_count;
file.nFactSize = (file.nFactSize >= this.nMin_size * 1024 * 1024 ? file.nFactSize : this.nMin_size * 1024 * 1024);
file.nFactSize = (file.nFactSize <= this.nMax_size * 1024 * 1024 ? file.nFactSize : this.nMax_size * 1024 * 1024);
file.nFactCount = Math.ceil(file.size / file.nFactSize);
file.complete = 0;
}
}
this.onSelect(this.fileFilter);
return this;
},
//
funDeleteFile: function (fileDelete) {
var arrFile = [];
for (var i = 0, file; i < this.fileFilter.length; iPP) {
file = this.fileFilter[i];
if (file != fileDelete) {
arrFile.push(file);
} else {
this.totalSize = this.totalSize - file.size;
this.onDelete(fileDelete);
}
}
this.fileFilter = arrFile;
return this;
},
onProgress: function () {},
uploadfile: function () {
this.uploadone(this.fileFilter[0]);
},
init: function () {
var self = this;
//
if (this.fileInput) {
this.fileInput.addEventListener("change", function (e) {
self.funGetFiles(e);
}, false);
}
},
uploadone: function (file) {
if (file.pause == true && file.index == this.current_upload) {
return;
}
if (file.end_upload == true) {
if (typeof (this.fileFilter[file.index + 1]) !== "undefined") {
this.current_upload = file.index + 1;
this.uploadone(this.fileFilter[file.index + 1]);
}
return;
}
var fData = new FormData();
var self = this;
//
var start = file.nCountNum * file.nFactSize;
var end = Math.min(start + file.nFactSize, file.size);
fData.append("file", file.slice(start, end));
fData.append("name", file.name);
fData.append("size", file.size);
fData.append("totalCount", file.nFactCount);
fData.append("indexCount", file.nCountNum);
fData.append("content", file.content);
for (var i in self.formData) {
fData.append(i, self.formData[i]);
}
var xhr = new XMLHttpRequest();
t1 = setTimeout(function () {
self.errormsg = "";
xhr.abort();
return false;
}, self.timeout);
//
xhr.onreadystatechange = function (e) {
if (xhr.readyState == 4) {
if (t1) {
clearTimeout(t1);
}
if (xhr.responseText) {
var res = JSON.parse(xhr.responseText);
if (xhr.status != 200 || res.status == 0) {
self.error = 1;
self.onFailure(res.msg);
self.funPause(file);
return false;
}
}
}
};
xhr.addEventListener("progress", function (e) {
// self.onProgress(file, e);
}, false);
xhr.addEventListener("load", function (e) {
self.onLoad(file, e);
}, false);
xhr.addEventListener("error", function () {
self.onFailure(""); //
self.funPause(file);
}, false);
xhr.addEventListener("abort", function () {
if (self.errormsg) {
var msg = self.errormsg;
} else {
var msg = "";
}
self.onFailure(msg); //
self.funPause(file);
}, false);
if (self.url.indexOf("?") >= 0) {
var url = self.url + "&rand=" + Math.random();
} else {
var url = self.url + "?rand=" + Math.random();
}
xhr.open("POST", url, true);
xhr.send(fData);
},
onLoad: function (file, e) {
if(this.error == 1){
return false;
};
var res = JSON.parse(e.target.responseText);
var upSize = (file.nCountNum + 1) * file.nFactSize;
upSize = upSize > file.size ? file.size : upSize;
file.upSize = upSize;
this.uploadSize += file.upSize;
this.uploadSize = this.uploadSize > this.totalSize ? this.totalSize : this.uploadSize;
this.onProgress(file, e, res);
if (file.nCountNum + 1 != file.nFactCount)
{
if (res.status == 1) {
//
file.nCountNumPP;
this.uploadone(file);
return;
}
if (res.status == 0) {
// this.onFailure(res.msg);
return false;
}
} else {
//
if (res.status == 0) {
// this.onFailure(res.msg);
return false;
}
file.end_upload = true;
if (typeof (this.fileFilter[file.index + 1]) !== "undefined") {
this.current_upload = file.index + 1;
this.uploadone(this.fileFilter[file.index + 1]);
return;
}
var self = this;
if (this.uploadSize == this.totalSize) {
setTimeout(function () {
self.onComplete();
}, 500);
}
}
},
funPause: function (file) {
file.pause = true;//
this.onPause(file);
}
};
this is the js of file segmentation
background processing is to read the uploaded file block and then complete the file merge by appending it