Webuploader 分片上傳多個大文件json
<div id="uploader" class="wu-example"> <div class="btns" style="margin-top: 20px;"> <div id="picker">選擇文件</div> <button id="ctlBtn" class="btn btn-default">開始上傳</button> </div> <!--用來存放文件信息--> <div id="thelist" class="uploader-list"></div> </div> <input type="hidden" id="guid" value="@Guid.NewGuid().ToString()" />
var uploader = WebUploader.create({ // swf文件路徑 swf: '/Webuploader/Uploader.swf', // 文件接收服務端。 server: '/Webuploader/fileupload.ashx?guid=' + $("#guid").val() + '', // 選擇文件的按鈕。可選。 // 內部根據當前運行是建立,多是input元素,也多是flash. pick: '#picker', // 不壓縮image, 默認若是是jpeg,文件上傳前會壓縮一把再上傳! resize: false, //開啓分片上傳 chunked: true, //上傳併發數 threads: 1, accept: { title: 'file', extensions: 'jpg,mp4,pdf,png,ppt,doc,xls,docx,xlsx,pptx', mimeTypes: 'image/jpeg,video/mp4,application/pdf,image/png,application/vnd.ms-powerpoint,application/msword,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.openxmlformats-officedocument.presentationml.presentation' } }); // 當有文件被添加進隊列的時候 uploader.on('fileQueued', function (file) { $("#thelist").append('<div id="' + file.id + '" class="item">' + '<h4 class="info">' + file.name + '</h4>' + '<p class="state">等待上傳...</p>' + '</div>'); }); //上傳成功事件 uploader.on('uploadSuccess', function (file) { $('#' + file.id).find('p.state').text('上傳成功'); }); //上傳失敗事件 uploader.on('uploadError', function (file) { $('#' + file.id).find('p.state').text('上傳出錯') }); uploader.on('uploadComplete', function (file) { $('#' + file.id).find('.progress').fadeOut(); }); //開始上傳事件 $("#ctlBtn").on('click', function () { if ($(this).hasClass('disabled')) { return false; } uploader.upload(); });
//若是進行了分片 if (context.Request.Form.AllKeys.Any(m => m == "chunk")) { //取得chunk和chunks int chunk = Convert.ToInt32(context.Request.Form["chunk"]); int chunks = Convert.ToInt32(context.Request.Form["chunks"]); //若是不存在就建立file文件夾 if (Directory.Exists(context.Server.MapPath("~/Resources/temp/")) == false) { Directory.CreateDirectory(context.Server.MapPath("~/Resources/temp/")); } //根據GUID建立用該GUID命名的臨時文件 string path = context.Server.MapPath("~/Resources/temp/" + context.Request["guid"]); FileStream addFile = new FileStream(path, FileMode.Append, FileAccess.Write); BinaryWriter AddWriter = new BinaryWriter(addFile); //得到上傳的分片數據流 file = context.Request.Files[i]; Stream stream = file.InputStream;
BinaryReader TempReader = new BinaryReader(stream); //將上傳的分片追加到臨時文件末尾 AddWriter.Write(TempReader.ReadBytes((int)stream.Length)); //關閉BinaryReader文件閱讀器 TempReader.Close(); stream.Close(); AddWriter.Close(); addFile.Close(); TempReader.Dispose(); stream.Dispose(); AddWriter.Dispose(); addFile.Dispose(); //若是是最後一個分片,則重命名臨時文件爲上傳的文件名 if (chunk == (chunks - 1)) { Url = ResourceConversionURL(context.Request.Files[i].FileName); //文件存在,進行刪除 if (File.Exists(context.Server.MapPath(Url))) { File.Delete(context.Server.MapPath(Url)); } FileInfo fileinfo = new FileInfo(path); fileinfo.MoveTo(context.Server.MapPath(Url)); //清空文件夾下全部的臨時文件 DirectoryInfo dir = new DirectoryInfo(context.Server.MapPath("~/Resources/temp/")); dir.Delete(true); } } //不是分片上傳的 else { file = context.Request.Files[i]; if (file == null || file.ContentLength == 0 || string.IsNullOrEmpty(file.FileName)) continue; filename = Path.GetFileName(file.FileName); Url = ResourceConversionURL(filename); file.SaveAs(HttpContext.Current.Server.MapPath(Url)); } var _result = "{\"jsonrpc\" : \"2.0\", \"result\" : null, \"id\" : \"" + filename + "\"}"; context.Response.Write(_result);