function UploadFileExcel() { var file = $("#file_upload")[0].files[0]; var form = new FormData(); form.append("file", file); form.append("uid", uid); form.append("token", token); $.ajax({ url: eshopUrl + "/index.php/Adminpc/V1/Ms/MsCard/inPool", type: "POST", contentType: "multipart/form-data", data: form, async: false, //異步 processData: false, //很重要,告訴jquery不要對form進行處理 contentType: false, //很重要,指定爲false才能造成正確的Content-Type success: function(res) { if(res.result == 1) { } else { } }, error: function() { AlertMsg("鏈接網絡錯誤,請稍後再試"); } }); $('#file_upload').replaceWith('<input class="up-file-input" id="file_upload" type="file" onchange="UploadFileExcel();" onchange="UploadFileExcel();" accept=".xlsx,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"/>'); }
分片上傳文件php
var file = $("#file")[0].files[0], //文件對象 name = file.name, //文件名 size = file.size, //總大小 succeed = 0, i = 0; var shardSize = 2 * 1024 * 1024, //以2MB爲一個分片 shardCount = Math.ceil(size / shardSize); //總片數 for(; i < shardCount; i++) { //計算每一片的起始與結束位置 var start = i * shardSize, end = Math.min(size, start + shardSize); //構造一個表單,FormData是HTML5新增的 var form = new FormData(); form.append("file", file.slice(start, end)); //slice方法用於切出文件的一部分 form.append("name", name); form.append("total", shardCount); //總片數 form.append("index", i + 1); //當前是第幾片 form.append("md5", md5); //當前是第幾片 //Ajax提交 $.ajax({ url: eshopUrl + "/index.php/Adminpc/V1/Setting/Version/uploadFile", type: "POST", contentType: "multipart/form-data", data: form, async: false, //異步 processData: false, //很重要,告訴jquery不要對form進行處理 contentType: false, //很重要,指定爲false才能造成正確的Content-Type success: function(res) { succeed++; $("#output").text(succeed + " / " + shardCount); if(succeed == shardCount) { document.getElementById("path").value = res.data; } } }); } $(".loading").hide();