<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>JS分片上傳-極速上傳</title> </head> <body> <input type="file" name="slice" id="slice" > <div id="output"><!-- 信息存放地 --> </div> <br/> </body> <script src="http://libs.baidu.com/jquery/1.8.3/jquery.min.js"></script> <script type="text/javascript"> $("#slice").change(function(event) { var file = $("#slice")[0].files[0]; var name = file.name; //進行初始化 $.ajax({ type:"POST", //請求方式 url:"http://localhost:8080/getuploadId", async: false, dataType:"json", //返回數據類型 data:{//請求參數 fileName:name, }, success:function(data){ //請求成功後 console.log("data.code="+JSON.stringify(data)); console.log("data.data.objectName="+data.data.objectName); console.log("data.data.uploadId="+data.data.uploadId); var objectName=data.data.objectName; var uploadId=data.data.uploadId; //成功後執行分段上傳 PostFile(file,0,objectName,uploadId); //合併 combineFile(objectName,uploadId); }, error:function (){ alert("系統出現異常!"); flag=false; } }); }); //執行分片上傳 function PostFile(file,i,objectName,uploadId){ var name = file.name, //文件名 size = file.size, //總大小shardSize = 2 * 1024 * 1024, shardSize = 1 * 1024 * 1024, //以2MB爲一個分片,每一個分片的大小 shardCount = Math.ceil(size / shardSize); //總片數 if(i >= shardCount){ return; } //console.log(size,i+1,shardSize); //文件總大小,第一次,分片大小// var start = i * shardSize; var end = start + shardSize; var packet = file.slice(start, end); //將文件進行切片 // 分段號 var partNumber = i + 1; /* 構建form表單進行提交 */ var form = new FormData(); form.append("fileId", '001') form.append("data", packet); //slice方法用於切出文件的一部分 // form.append("lastModified", file.lastModified); //最後的額修改時間 form.append("name", name); form.append("totalSize", size); form.append("uploadId", uploadId); form.append("objectName", objectName); form.append("mainFile", packet); form.append("total", shardCount); form.append("sortNumber", partNumber); //uploadId:uploadId, //objectName:objectName, //mainFile:blob, //total:partCount, //sortNumber:partNumber if(shardCount === i+1){ shardSize = size - i * shardSize } form.append("shardSize", shardSize); form.append("total", shardCount); //總片數 form.append("index", i + 1); //當前是第幾片 $.ajax({ // header: { // web_token: "cpBvqWrr0UK9zPJoKj+412alxxcY6qFwrSVoO0juZg0S1jX/sGfMd9oi3AsSxW+MdWFrwtGCykUTmMqrqpbCElmT75DfB87lENC7lCuMxgF0d5wQB6yAubUVyGOzhbrScvIfD8ZSjOJ3D88AOy2xRd4oG8TxeGoMoZqnqIwjKg=" // }, url: "http://localhost:8080/multipartupload", type: "POST", data: form, //timeout:"10000", //超時10秒 async: false, //同步 dataType:"json", processData: false, //很重要,告訴jquery不要對form進行處理 contentType: false, //很重要,指定爲false才能造成正確的Content-Type success: function (data) { console.log("data="+JSON.stringify(data)); /* 表示上一塊文件上傳成功,繼續下一次 */ if (data.data.code == 1) { form = ''; i++; PostFile(file, i,objectName,uploadId); } else if (data.data.code == 502) { form = ''; /* 失敗後,每2秒繼續傳一次分片文件 */ setInterval(function () { PostFile(file, i,objectName,uploadId) }, 2000); } else if (data.data.code == 200) { console.log("上傳成功"); } else if (data.data.code == 500) { console.log('第'+msg.i+'次,上傳文件有誤!'); } else { console.log('未知錯誤'); } } }) } //合併 function combineFile(objectName,uploadId){ console.log("objectName="+objectName); console.log("uploadId="+uploadId); var object=new Object(); object.objectName=objectName; object.uploadId=uploadId; $.ajax({ headers: { Accept: "application/json; charset=utf-8" }, type:"POST", //請求方式 url:"http://localhost:8080/combine", async: false, contentType:"application/json", dataType:"json", //返回數據類型 data:JSON.stringify(object), success:function(data){ //請求成功後 console.log("data="+JSON.stringify(data)); console.log("data.data.code="+data.data.code); console.log("data.data.url="+data.data.url); }, error:function (){ alert("系統出現異常!"); flag=false; } }); } </script> </html>