本篇使用客戶端jQuery-File-Upload插件和服務端Badkload組件實現多文件異步上傳。MVC文件上傳相關兄弟篇:javascript
MVC文件上傳01-使用jquery異步上傳並客戶端驗證類型和大小
MVC文件上傳02-使用HttpPostedFileBase上傳多個文件
MVC文件上傳03-使用Request.Files上傳多個文件 css
處理文件上傳的服務端組件Backloadhtml
用於處理文件上傳的服務端組件。Backload與客戶端文件上傳插件(好比jQuery-File-Upload)配合使用,初步造成一個處理文件異步上傳的解決方案。html5
□ Badkload的一些功能和特色java
● 零配置:Backload提供默認BackloadDemoController用來處理上傳文件請求。若是其它控制器也想擁有"Backload處理上傳文件的超強能力",只須要繼承BackloadDemoController就能夠。
● 在web.config中顯式配置
● 支持多種存儲位置:文件夾、數據庫(經過Entity Framework)、雲存儲(未來支持)
● 根據不一樣的請求上下文把上傳文件保存到不一樣的位置
● 根據上傳文件的不一樣類型保存到不一樣的位置
● 支持對圖片的裁剪,而參數能夠在web.confgig中配置
● 支持對多個圖片的處理
● 支持圖片類型轉換
● 支持生成縮略圖
● 支持在服務端建立惟一的文件名(GUIDs)
● 還能夠在原始文件和修改文件之間創建映射,並返回給客戶端一個友好的名稱。
● 良好的可擴展性:經過實現Badkload的接口和使用ExportAttribute特性。
● 安全性:爲控制器加上驗證和受權
● 可調試:方便斷點調試發現錯誤
...jquery
□ 安裝客戶端jQuery-File-Upload插件和服務端Badkload組件,有2種方式:git
一、分別安裝客戶端和服務端所需的插件和組件github
● 安裝服務端的Backload:web
PM> Install-Package Backloadchrome
● 若是客戶端選擇使用jQuery-File-Upload插件,經過以下來安裝這個客戶端文件上傳插件:
PM> Install-Package JQuery_File_Upload_Plugin
二、安裝Query-File-Upload結合Backload實現文件上傳的Demo,一次性安裝客戶端和服務端所需的插件和組件:
PM> Install-Package JQueryFileUpload_Demo_with_Backload
例子:零配置實現多文件上傳
□ 新建一個MVC4項目,工具--庫程序包管理器--程序包管理器控制檯,輸入:
PM> Install-Package JQueryFileUpload_Demo_with_Backload
引用文件夾多了以下文件:

App_Start文件夾裏多了以下BackloadConfig.cs文件,裏面有jQuery File Upload插件全部須要的js和css文件:

Content文件夾多了以下文件:

Controller文件夾多了一個控制器:

Scripts文件夾多了以下文件:

Views文件夾多了以下文件夾和文件:

□ 運行http://localhost:1631/BackloadDemo/Index,發現以下問題:
一、工具--庫程序包管理器--程序包管理器控制檯,被禁用
關閉VS,從新打開就恢復。
二、報錯
未能加載文件或程序集「WebGrease, Version=1.5.1.25624, Culture=neutral, PublicKeyToken=31bf3856ad364e35」或它的某一個依賴項。找到的程序集清單定義與程序集引用不匹配。 (異常來自 HRESULT:0x80131040)
經過"uninstall-package -f WebGrease"先卸載,而後經過"install-package WebGrease"再下載最新版本,問題解決。
□ 再次運行http://localhost:1631/BackloadDemo/Index

□ 添加2個上傳文件,出現上傳文件列表並配有縮略圖

□ 分別點擊"Start"按鈕,上傳成功後,"Start"按鈕消失,出現"Delete"按鈕。

□ 根目錄下多了Files文件夾

□ File文件夾下不只有上傳的圖片,還有一個存放縮略圖的文件夾_thumbs:

□ 勾選上傳文件列表中的一行,點擊"Delete":
界面少了一行記錄:

File文件夾下原先的圖片也被刪除:

總結
● 僅僅經過PM> Install-Package JQueryFileUpload_Demo_with_Backload 安裝了客戶端文件上傳插件jQuery-File-Upload和服務端文件處理組件Backload,沒有作任何配置,就能夠進行多個文件的上傳。
● 文件默認被上傳到根目錄下的Files文件夾,須要這個文件夾的"寫"權限。
□ BackloadDemoController是默認的控制器
1: using System.Web.Mvc;
2:
3: namespace MvcApplication6.Controllers
4: {
5: public class BackloadDemoController : Controller
6: {
7: //
8: // GET: /BackupDemo/
9: public ActionResult Index()
10: {
11: return View();
12: }
13: }
14: }
□ BackloadDemoController是默認的上傳文件視圖
展開@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>JQuery File Upload Plugin with Backload demo installation</title>
@Styles.Render("~/Content/themes/base/css")
@Styles.Render("~/Content/css")
<!-- We use Backloads. bundeling feature to register only those client side javascript and style files of the jQuery File Upload Plugin that are needed -->
@Styles.Render("~/bundles/fileupload/bootstrap/BasicPlusUI/css")
<!-- Bootstrap CSS fixes for IE6 -->
<!--[if lt IE 7]><link rel="stylesheet" href="/Content/FileUpload/css/bootstrap/bootstrap-ie6.debug.css"><![endif]-->
<!--[if lt IE 9]><script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><![endif]-->
</head>
<body>
<div>
<!-- The file upload form used as target for the file upload widget -->
<form id="fileupload" action="/Backload/UploadHandler" method="POST" enctype="multipart/form-data">
<!-- Redirect browsers with JavaScript disabled to the origin page -->
<noscript><input type="hidden" name="redirect" value="/"></noscript>
<!-- The fileupload-buttonbar contains buttons to add/delete files and start/cancel the upload -->
<div class="row fileupload-buttonbar">
<div class="span7">
<!-- The fileinput-button span is used to style the file input field as button -->
<span class="btn btn-success fileinput-button">
<i class="icon-plus icon-white"></i>
<span>Add files...</span>
<input type="file" name="files[]" multiple>
</span>
<button type="submit" class="btn btn-primary start">
<i class="icon-upload icon-white"></i>
<span>Start upload</span>
</button>
<button type="reset" class="btn btn-warning cancel">
<i class="icon-ban-circle icon-white"></i>
<span>Cancel upload</span>
</button>
<button type="button" class="btn btn-danger delete">
<i class="icon-trash icon-white"></i>
<span>Delete</span>
</button>
<input type="checkbox" class="toggle">
<!-- The loading indicator is shown during file processing -->
<span class="fileupload-loading"></span>
</div>
<!-- The global progress information -->
<div class="span5 fileupload-progress fade">
<!-- The global progress bar -->
<div class="progress progress-success progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100">
<div class="bar" style="width:0%;"></div>
</div>
<!-- The extended global progress information -->
<div class="progress-extended"> </div>
</div>
</div>
<!-- The table listing the files available for upload/download -->
<table role="presentation" class="table table-striped"><tbody class="files" data-toggle="modal-gallery" data-target="#modal-gallery"></tbody></table>
</form>
<!-- The template to display files available for upload -->
<script id="template-upload" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-upload fade">
<td>
<span class="preview"></span>
</td>
<td>
<p class="name">{%=file.name%}</p>
{% if (file.error) { %}
<div><span class="label label-important">Error</span> {%=file.error%}</div>
{% } %}
</td>
<td>
<p class="size">{%=o.formatFileSize(file.size)%}</p>
{% if (!o.files.error) { %}
<div class="progress progress-success progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0"><div class="bar" style="width:0%;"></div></div>
{% } %}
</td>
<td>
{% if (!o.files.error && !i && !o.options.autoUpload) { %}
<button class="btn btn-primary start">
<i class="icon-upload icon-white"></i>
<span>Start</span>
</button>
{% } %}
{% if (!i) { %}
<button class="btn btn-warning cancel">
<i class="icon-ban-circle icon-white"></i>
<span>Cancel</span>
</button>
{% } %}
</td>
</tr>
{% } %}
</script>
<!-- The template to display files available for download -->
<script id="template-download" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-download fade">
<td>
<span class="preview">
{% if (file.thumbnail_url) { %}
<a href="{%=file.url%}" title="{%=file.name%}" data-gallery="gallery" download="{%=file.name%}"><img src="{%=file.thumbnail_url%}"></a>
{% } %}
</span>
</td>
<td>
<p class="name">
<a href="{%=file.url%}" title="{%=file.name%}" data-gallery="{%=file.thumbnail_url&&'gallery'%}" download="{%=file.name%}">{%=file.name%}</a>
</p>
{% if (file.error) { %}
<div><span class="label label-important">Error</span> {%=file.error%}</div>
{% } %}
</td>
<td>
<span class="size">{%=o.formatFileSize(file.size)%}</span>
</td>
<td>
<button class="btn btn-danger delete" data-type="{%=file.delete_type%}" data-url="{%=file.delete_url%}"{% if (file.delete_with_credentials) { %} data-xhr-fields='{"withCredentials":true}'{% } %}>
<i class="icon-trash icon-white"></i>
<span>Delete</span>
</button>
<input type="checkbox" name="delete" value="1" class="toggle">
</td>
</tr>
{% } %}
</script>
</div>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryui")
<!-- We use Backloads. bundeling feature to register only those client side javascript and style files of the jQuery File Upload Plugin that are needed -->
@Scripts.Render("~/bundles/fileupload/bootstrap/BasicPlusUI/js")
<!-- Initialize the jQuery File Upload Plugin -->
<script src="~/Scripts/FileUpload/backload.demo.js"></script>
</body>
</html>
以上,html部分:
● <form id="fileupload" action="/Backload/UploadHandler" method="POST" enctype="multipart/form-data">放置添加、取消、上傳等按鈕
● <script id="template-upload" type="text/x-tmpl">上傳文件列表
● 下載文件列表
css部分:
● @Styles.Render("~/Content/themes/base/css")
● @Styles.Render("~/Content/css")
● @Styles.Render("~/bundles/fileupload/bootstrap/BasicPlusUI/css") 使用Backloads的bundeling功能註冊jQuery File Upload所需的css
js部分:
● @Scripts.Render("~/bundles/jquery")
● @Scripts.Render("~/bundles/jqueryui")
● @Scripts.Render("~/bundles/fileupload/bootstrap/BasicPlusUI/js") 使用Backloads的bundeling功能註冊jQuery File Upload所需的js
● script src="~/Scripts/FileUpload/backload.demo.js"></script> 初始化jQuery File Upload
參考資料:
※ http://backload.org/ Backload官網
※ https://github.com/blackcity/Backload#examples Backload例子
※ http://nuget.org/packages/Backload/ nuget上的Backload
※ http://blueimp.github.io/jQuery-File-Upload/ jQuery File Upload官網
※ https://github.com/blueimp/jQuery-File-Upload/wiki github上的jQuery File Upload介紹
※ https://github.com/blueimp/jQuery-File-Upload github上的jQuery File Upload源碼
※ https://www.nuget.org/packages/JQueryFileUpload_Demo_with_Backload/ 下載jQuery File Upload結合Backload的MVC案例