验证中...
前端代码
Raw Copy
var arr=[];
var uploadErrorNum =0;
$(function(){
function upload(file,currShard,shardCount){//文件 当前片 总片数
var size = file.size; //总大小
var shardSize = 10 * 1024 * 1024; //以2MB为一个分片
var end = Math.min(size, (currShard+1)*shardSize); //当前片结束位置
var form = new FormData();
form.append("file", file.slice(currShard*shardSize,end)); //slice方法用于切出文件的一部分
form.append("total", shardCount); //总片数
form.append("currShard", currShard+1); //当前是第几片
form.append("prefix",".part"+(currShard+1));
form.append("name",file.name+new Date().getTime());
if(currShard+1==shardCount){
form.append("names",arr);
}
//Ajax提交
$.ajax({
url: "commMaster!uploadMasterVideo",
type: "POST",
data: form,
processData: false, //很重要,告诉jquery不要对form进行处理
contentType: false, //很重要,指定为false才能形成正确的Content-Type
xhr:function(){
myXhr = $.ajaxSettings.xhr();
if(myXhr.upload){ // check if upload property exists
myXhr.upload.addEventListener('progress',function(e){
var uploaded = e.loaded;
var tot = e.total;
//console.log(uploaded/tot/shardCount+((currShard)/shardCount));//上传的总百分比
}, false); // for handling the progress of the upload
}
return myXhr;
},
success: function(data){
currShard++;
if(currShard>=shardCount){
arr=[];
uploadErrorNum=0;
alert("over");
}else{
if(data.msg=="success"){
arr.push(data.data.videoName);
upload(file,currShard,shardCount);
}else{
uploadErrorNum++;
if(uploadErrorNum>5){
alert('上传失败次数超过5次,停止上传。失败片位置'+currShard);
}else{//失败重新上传
upload(file,currShard--,shardCount);
}
}
}
}
});
}
$('#upload').click(function(){
var file = $("#file")[0].files[0]; //文件对象
var shardSize = 10 * 1024 * 1024; //以10MB为一个分片 可自定义
var shardCount = Math.ceil(file.size/shardSize);
var currShard=0;
//计算每一片的起始与结束位置
if(file.size<=shardSize){
alert("该文件不需要进行分片上传");
return ;
}
upload(file,currShard,shardCount);
})
});

Comment list( 0 )

You need to Sign in for post a comment

Help Search