|
@@ -14,15 +14,18 @@
|
|
|
* @param {[type]} count [上传的次数]
|
|
|
* @param {[type]} counts [上传的总次数]
|
|
|
* @param {[string]} uploadLink [上传链接] 默认
|
|
|
+ * @param {[type]} fileHash [文件Hash]
|
|
|
*/
|
|
|
- function loop(file, totalSize, eachSize, SucCallback, ErrCallback, count, counts, uploadLink){
|
|
|
+ function loop(file, totalSize, eachSize, SucCallback, ErrCallback, count, counts, fileHash, uploadLink){
|
|
|
var thunkFrom = count * eachSize;// 切割起始端
|
|
|
var thunkTo = (count + 1) * eachSize > totalSize ? totalSize : (count + 1) * eachSize;// 切割末端
|
|
|
|
|
|
fd = new FormData();
|
|
|
fd.append('theFile', file.slice(thunkFrom, thunkTo)); // 分好段的文件
|
|
|
fd.append('fileName', file.name); // 文件名
|
|
|
+ fd.append('fileHash', fileHash); // 文件名
|
|
|
fd.append('totalSize', totalSize); // 大小
|
|
|
+ fd.append('fileChunk', count); // 大小
|
|
|
fd.append('isFirstChunk', count === 0); // 是否第一次上传
|
|
|
fd.append('isLastChunk', count === counts - 1); // 是否最后一次上传
|
|
|
|
|
@@ -34,13 +37,13 @@
|
|
|
if(xhr.readyState==4 && xhr.status==200){
|
|
|
var rs = JSON.parse(xhr.responseText);
|
|
|
if(rs.status == 200){
|
|
|
- SucCallback(thunkTo, totalSize)
|
|
|
+ SucCallback(thunkTo, totalSize, rs.link)
|
|
|
// isLastChunk
|
|
|
if(count === counts - 1){
|
|
|
return;
|
|
|
}
|
|
|
// 继续上传
|
|
|
- loop(file, totalSize, eachSize, SucCallback, ErrCallback, ++count, counts, uploadLink)
|
|
|
+ loop(file, totalSize, eachSize, SucCallback, ErrCallback, ++count, counts, fileHash, uploadLink)
|
|
|
}else{
|
|
|
ErrCallback(rs)
|
|
|
}
|
|
@@ -48,20 +51,48 @@
|
|
|
};
|
|
|
xhr.send(fd);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 文件是否存在
|
|
|
+ * @param hash hash
|
|
|
+ * @param callback 回调
|
|
|
+ */
|
|
|
+ function fileIsExists(hash, callback) {
|
|
|
+ U.A.Request('http://cd.1473.cn/php', (['db.1473.cn', 'UseStudio_Upload', "selectFileMsgWhereFileHash", hash]), function (r) {
|
|
|
+ if(r.value[0]){
|
|
|
+ callback(r.value[0])
|
|
|
+ }else{
|
|
|
+ callback(false)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
/**
|
|
|
* 分割上传
|
|
|
* @param {[type]} file [文件Flie对象]
|
|
|
+ * @param {[type]} fileHash [文件Hash]
|
|
|
* @param {[function]} SucCallback [成功回调]
|
|
|
* @param {[function]} ErrCallback [失败回调]
|
|
|
* @param {[string]} uploadLink [上传链接] default
|
|
|
*/
|
|
|
- return function(file, SucCallback, ErrCallback, uploadLink) {
|
|
|
- uploadLink = uploadLink || '/upload';
|
|
|
- var totalSize = file.size; // 总大小
|
|
|
- var eachSize = 1024 * 1000; // 每次上传大小 这里是100kb
|
|
|
- var counts = Math.ceil(totalSize / eachSize); // 上传的总次数
|
|
|
+ return function(file, fileHash, SucCallback, ErrCallback, uploadLink) {
|
|
|
+ uploadLink = uploadLink || '/upload';
|
|
|
+ var totalSize = file.size; // 总大小
|
|
|
+ var eachSize = 1024 * 100; // 每次上传大小 这里是100kb
|
|
|
+ var counts = Math.ceil(totalSize / eachSize); // 上传的总次数
|
|
|
|
|
|
- loop(file, totalSize, eachSize, SucCallback, ErrCallback, 0, counts, uploadLink)
|
|
|
+ fileIsExists(fileHash, function(data){
|
|
|
+ if(data === false){
|
|
|
+ loop(file, totalSize, eachSize, SucCallback, ErrCallback, 0, counts, fileHash, uploadLink)
|
|
|
+ } else {
|
|
|
+ if(data.Finish == '1'){
|
|
|
+ SucCallback(totalSize, totalSize)
|
|
|
+ }else{
|
|
|
+ // 存在的文件,获取上次上的分片数目,继续上传
|
|
|
+ loop(file, totalSize, eachSize, SucCallback, ErrCallback, ~~data.Chunk+1, counts, fileHash, uploadLink)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
//test
|
|
|
/*size > 1024
|
|
|
? size / 1024 > 1024
|