前臺代碼javascript
<div id="wrapper">java
<div id="container">app
<!--頭部,相冊選擇和格式選擇-->post
<div id="uploader">ui
<div class="queueList">spa
<div id="dndArea" class="placeholder">orm
<div id="filePicker"></div>server
</div>排序
</div>圖片
<div class="statusBar" style="display:none;">
<div class="progress">
<span class="text">0%</span>
<span class="percentage"></span>
</div><div class="info"></div>
<div class="btns">
<div id="filePicker2"></div><div class="uploadBtn">開始上傳</div>
</div>
</div>
</div>
</div>
</div>
後臺代碼
[HttpPost]
public ActionResult ChunkUpload()//切片
{
double n = 100.0 / 500.0;
string newFolderName = ((n + 1) * 2000).ToString();
//若是進行了分片
if (Request.Form.AllKeys.Any(m => m == "chunk"))
{
//取得chunk和chunks
int chunk = Convert.ToInt32(Request.Form["chunk"]);//當前分片在上傳分片中的順序(從0開始)
int chunks = Convert.ToInt32(Request.Form["chunks"]);//總分片數
//根據GUID建立用該GUID命名的臨時文件夾
string folder = Path.Combine(HttpRuntime.AppDomainAppPath, "Media\\Default\\MediaLibrary/" + newFolderName + "/" + Request["guid"] + "/");
if (!System.IO.Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
string path = folder + chunk;
//創建臨時傳輸文件夾
if (!Directory.Exists(Path.GetDirectoryName(folder)))
{
Directory.CreateDirectory(folder);
}
FileStream addFile = new FileStream(path, FileMode.Append, FileAccess.Write);
BinaryWriter AddWriter = new BinaryWriter(addFile);
//得到上傳的分片數據流
var file = Request.Files[0];
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();
return Json(new { chunked = true, hasError = false, f_ext = Path.GetExtension(file.FileName) });
}
else//沒有分片直接保存
{
Request.Files[0].SaveAs(Path.Combine(HttpRuntime.AppDomainAppPath, "Media\\Default\\MediaLibrary/" + newFolderName + "/" + DateTime.Now.ToFileTime() + Path.GetExtension(Request.Files[0].FileName)));
return Json(new { chunked = true, hasError = false });
}
}
[HttpPost]
public ActionResult Layer(FormCollection collection)//整合切片 刪除切片
{
try
{
double n = 100.0 / 500.0;
string newFolderName = ((n + 1) * 2000).ToString();
if (Request.Form.AllKeys.Any(m => m == "chunk"))//判斷是否切片
{
var guid = Request["guid"];//GUID
var uploadDir = Path.Combine(HttpRuntime.AppDomainAppPath, "Media\\Default\\MediaLibrary/" + newFolderName);//Upload 文件夾
if (!System.IO.Directory.Exists(uploadDir))
{
Directory.CreateDirectory(uploadDir);
}
var dir = Path.Combine(uploadDir, guid);//臨時文件夾
var ext = Path.GetExtension(Request["fileName"]);//獲取文件後綴名
var files = Directory.GetFiles(dir);//得到下面的全部切片文件
var name = DateTime.Now.ToFileTime().ToString() + 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
{
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 = "Media\\Default\\MediaLibrary/" + newFolderName + "/" + name });
}
return Json(new { r = 1, path = "Media\\Default\\MediaLibrary/" + newFolderName + "/" + DateTime.Now.ToFileTime() + Path.GetExtension(Request["fileName"]) });
}
catch (Exception ex)
{
return Json(new { r = 0, err = ex.Message });
}
}
javascript主要代碼 (upload.js內)
var GUID = WebUploader.Base.guid();//一個GUID
// 實例化
uploader = WebUploader.create({
pick: {
id: '#filePicker',
label: '點擊選擇文件',
innerHTML: '上傳',
},
formData: {
uid: 123
},
dnd: '#dndArea',
paste: '#uploader',
swf: '~/Scripts/Uploader.swf',//路徑
chunked: true,//是否容許切片
chunkSize: 2*1024 * 1024,//單個切片大小
server: '/Ekm/ChunkUpload',//控制器方法
method:'POST',
// runtimeOrder: 'flash',
threads: true,
chunkRetry: 2,
// accept: {
// title: 'Images',
// extensions: 'gif,jpg,jpeg,bmp,png',
// mimeTypes: 'image/*'
// },
compress: false,
// 禁掉全局的拖拽功能。這樣不會出現圖片拖進頁面的時候,把圖片打開。
disableGlobalDnd: true,
formData: {
guid: GUID //自定義參數
},
fileNumLimit: 50, //最大文件存儲數量
fileSizeLimit: 400 * 1024 * 1024, // 200 M 所有文件最大
fileSingleSizeLimit:100 * 1024 * 1024, // 50 M 單個文件最大
duplicate: false,//是否重複上傳文件 true 重複 false 不重複
});
uploader.on('uploadSuccess', function (file, response) {整合切片
//合併文件
$.post('/Ekm/Layer', { guid: GUID, fileName: file.name }, function (data) {
if (data.r == 1) {
alert("上傳完成");
}
else {
alert(data.err);
}
});
});
雲盤地址:連接: https://pan.baidu.com/s/1nxl89fF 密碼: dd9z