首先是前端:前端
1 var GUID = WebUploader.Base.guid();//一個GUID 2 uploadereditsVideo = WebUploader.create({ 3 // swf文件路徑 4 swf: '../../Scripts/plugins/webuploader/Uploader.swf', 5 // 文件接收服務端。 6 server: '/AjaxUpload/ChunkUpload', 7 pick: { 8 id: '#picker', 9 label: '上傳', 10 innerHTML: '上傳', 11 multiple: false 12 }, 13 fileNumLimit: 1, 14 fileSingleSizeLimit: 1024 * 1024 *100, 15 chunked: true,//開始分片上傳 16 chunkSize: 1024 * 1024 * 2,//每一片的大小 17 formData: { 18 guid: GUID //自定義參數 19 } 20 }); 21 22 uploadereditsVideo.on('fileQueued', function (file) { 23 uploadereditsVideo.upload(); 24 }); 25 // 文件上傳成功 26 uploadereditsVideo.on('uploadSuccess', function (file, response) { 27 //合併文件 28 $.post('/AjaxUpload/Merge', { guid: GUID, fileName: file.name }, function (data) { 29 if (data.r == 1) { 30 alert("上傳完成"); 31 } 32 else { 33 alert(data.err); 34 } 35 }); 36 });
後端接收方法Uploadweb
1 public ActionResult Upload() 2 { 3 //若是進行了分片 4 if (Request.Form.AllKeys.Any(m => m == "chunk")) 5 { 6 //取得chunk和chunks 7 int chunk = Convert.ToInt32(Request.Form["chunk"]);//當前分片在上傳分片中的順序(從0開始) 8 int chunks = Convert.ToInt32(Request.Form["chunks"]);//總分片數 9 //根據GUID建立用該GUID命名的臨時文件夾 10 string folder = Server.MapPath("~/upload/" + Request["guid"] + "/"); 11 string path = folder + chunk; 12 13 //創建臨時傳輸文件夾 14 if (!Directory.Exists(Path.GetDirectoryName(folder))) 15 { 16 Directory.CreateDirectory(folder); 17 } 18 19 FileStream addFile = new FileStream(path, FileMode.Append, FileAccess.Write); 20 BinaryWriter AddWriter = new BinaryWriter(addFile); 21 //得到上傳的分片數據流 22 var file = Request.Files[0]; 23 Stream stream = file.InputStream; 24 25 BinaryReader TempReader = new BinaryReader(stream); 26 //將上傳的分片追加到臨時文件末尾 27 AddWriter.Write(TempReader.ReadBytes((int)stream.Length)); 28 //關閉BinaryReader文件閱讀器 29 TempReader.Close(); 30 stream.Close(); 31 AddWriter.Close(); 32 addFile.Close(); 33 34 TempReader.Dispose(); 35 stream.Dispose(); 36 AddWriter.Dispose(); 37 addFile.Dispose(); 38 return Json(new { chunked = true, hasError = false, f_ext = Path.GetExtension(file.FileName) }); 39 } 40 else//沒有分片直接保存 41 { 42 Request.Files[0].SaveAs(Server.MapPath("~/upload/" + DateTime.Now.ToFileTime() + Path.GetExtension(Request.Files[0].FileName))); 43 return Json(new { chunked = true, hasError = false }); 44 } 45 }
合併方法Merge後端
public ActionResult Merge() { try { var guid = Request["guid"];//GUID var uploadDir = Server.MapPath("~/upload");//Upload 文件夾 var dir = Path.Combine(uploadDir, guid);//臨時文件夾 var ext = Path.GetExtension(Request["fileName"]); var files = Directory.GetFiles(dir);//得到下面的全部文件 var name = Guid.NewGuid().ToString("N") + ext; var finalPath = Path.Combine(uploadDir, name);//最終的文件名 var fs = new FileStream(finalPath, FileMode.Create); foreach (var part in files.OrderBy(x => x.Length).ThenBy(x => x))//排一下序,保證從0-N Write { var bytes = System.IO.File.ReadAllBytes(part); fs.Write(bytes, 0, bytes.Length); bytes = null; System.IO.File.Delete(part);//刪除分塊 } fs.Flush(); fs.Close(); Directory.Delete(dir);//刪除文件夾 return Json(new { r = 1, path = "/upload/" + name }); } catch (Exception ex) { return Json(new { r = 0, err = ex.Message }); } }
以上就是分片上傳的方法ide