標籤:sys ons 線程 span 檔案路徑 value manage ppa created
1、首先載入
<link rel="stylesheet" type="text/css" href="~/Content/scripts/plugins/webuploader/webuploader.css">
<script type="text/javascript" src="~/Content/scripts/plugins/webuploader/webuploader.js"></script>
2、在頁面中放入DIV
<div id="uploader" class="wu-example">
<!--用來存放檔案資訊-->
<div id="thelist" class="uploader-list"></div>
<div class="btns">
<div id="picker">選擇檔案</div>
<input id="ctlBtn" type="button" value="開始上傳" class="btn btn-default" />
</div>
</div>
3、初始化 WebUploader
<script> var applicationPath = window.applicationPath === "" ? "" : window.applicationPath || "../../"; var GUID = WebUploader.Base.guid();//一個GUID $(function () { var $ = jQuery; var $list = $(‘#thelist‘); var uploader = WebUploader.create({ // 選完檔案後,是否自動上傳。 auto: false, // swf檔案路徑 swf: applicationPath + ‘../Content/scripts/plugins/webuploader/Uploader.swf‘, // 檔案接收服務端。 server: applicationPath + ‘BusinesManage/KnowledgeBase/Upload‘, // 選擇檔案的按鈕。可選。 // 內部根據當前運行是建立,可能是input元素,也可能是flash. pick: ‘#picker‘, chunked: true,//開始分區上傳 chunkSize: 2048000,//每一片的大小 formData: { guid: GUID //自訂參數,待會兒解釋 }, // 不壓縮image, 預設如果是jpeg,檔案上傳前會壓縮一把再上傳! resize: false }); // 當有檔案被添加進隊列的時候 uploader.on(‘fileQueued‘, function (file) { $list.append(‘<div id="‘ + file.id + ‘" class="item">‘ + ‘<h4 class="info">‘ + file.name + ‘</h4>‘ + ‘<p class="state">等待上傳...</p>‘ + ‘</div>‘); }); // 檔案上傳過程中建立進度條即時顯示。 uploader.on(‘uploadProgress‘, function (file, percentage) { var $li = $(‘#‘ + file.id), $percent = $li.find(‘.progress .progress-bar‘); // 避免重複建立 if (!$percent.length) { $percent = $(‘<div class="progress progress-striped active">‘ + ‘<div class="progress-bar" role="progressbar" style="width: 0%">‘ + ‘</div>‘ + ‘</div>‘).appendTo($li).find(‘.progress-bar‘); } $li.find(‘p.state‘).text(‘上傳中‘); $percent.css(‘width‘, percentage * 100 + ‘%‘); }); // 檔案上傳成功,給item添加成功class, 用樣式標記上傳成功。 uploader.on(‘uploadSuccess‘, function (file, response) { $(‘#‘ + file.id).find(‘p.state‘).text(‘已上傳‘); $.post(‘../../BusinesManage/KnowledgeBase/Merge‘, { guid: GUID, fileName: file.name }, function (data) { $("#uploader .state").html("上傳成功..."); }); }); // 檔案上傳失敗,顯示上傳出錯。 uploader.on(‘uploadError‘, function (file) { $(‘#‘ + file.id).find(‘p.state‘).text(‘上傳出錯‘); }); // 完成上傳完了,成功或者失敗,先刪除進度條。 uploader.on(‘uploadComplete‘, function (file) { $(‘#‘ + file.id).find(‘.progress‘).fadeOut(); }); //所有檔案上傳完畢 uploader.on("uploadFinished", function () { //提交表單 }); //開始上傳 $("#ctlBtn").click(function () { uploader.upload(); }); }); </script>
4、幕後處理
1 #region 檔案上傳 2 [HttpPost] 3 public ActionResult Upload() 4 { 5 string fileName = Request["name"]; 6 string fileRelName = fileName.Substring(0,fileName.LastIndexOf(‘.‘));//設定臨時存放檔案夾名稱 7 int index = Convert.ToInt32(Request["chunk"]);//當前分塊序號 8 var guid = Request["guid"];//前端傳來的GUID號 9 var dir = Server.MapPath("~/Upload");//檔案上傳目錄10 dir = Path.Combine(dir, fileRelName);//臨時儲存分塊的目錄11 if (!System.IO.Directory.Exists(dir))12 System.IO.Directory.CreateDirectory(dir);13 string filePath = Path.Combine(dir, index.ToString());//區塊檔案名為索引名,更嚴謹一些可以加上是否存在的判斷,防止多線程時並發衝突14 var data = Request.Files["file"];//表單中取得區塊檔案15 //if (data != null)//為null可能是暫停那一瞬間16 //{17 data.SaveAs(filePath);//報錯18 //}19 return Json(new { erron = 0 });//Demo,隨便返回了個值,請勿參考20 }21 public ActionResult Merge()22 {23 var guid = Request["guid"];//GUID24 var uploadDir = Server.MapPath("~/Upload");//Upload 檔案夾25 var fileName = Request["fileName"];//檔案名稱26 string fileRelName = fileName.Substring(0, fileName.LastIndexOf(‘.‘));27 var dir = Path.Combine(uploadDir, fileRelName);//臨時檔案夾 28 var files = System.IO.Directory.GetFiles(dir);//獲得下面的所有檔案29 var finalPath = Path.Combine(uploadDir, fileName);//最終的檔案名稱(demo中儲存的是它上傳時候的檔案名稱,實際操作肯定不能這樣)30 var fs = new FileStream(finalPath, FileMode.Create);31 foreach (var part in files.OrderBy(x => x.Length).ThenBy(x => x))//排一下序,保證從0-N Write32 {33 var bytes = System.IO.File.ReadAllBytes(part);34 fs.Write(bytes, 0, bytes.Length);35 bytes = null;36 System.IO.File.Delete(part);//刪除分塊37 }38 fs.Flush();39 fs.Close();40 System.IO.Directory.Delete(dir);//刪除檔案夾41 return Json(new { error = 0 });//隨便返回個值,實際中根據需要返回42 }43 #endregion
asp.net mvc5 WebUploader多檔案大檔案上傳