MVC檔案上傳-使用jQuery.FileUpload和Backload組件實現檔案上傳

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   os   使用   sp   java   

本篇使用用戶端jQuery-File-Upload外掛程式和服務端Badkload組件實現多檔案非同步上傳。MVC檔案上傳相關兄弟篇: 

  處理檔案上傳的服務端組件Backload

用於處理檔案上傳的服務端組件。Backload與用戶端檔案上傳外掛程式(比如jQuery-File-Upload)配合使用,初步形成一個處理檔案非同步上傳的解決方案。

□ Badkload的一些功能和特點

● 零配置:Backload提供預設BackloadDemoController用來處理上傳檔案請求。如果其它控制器也想擁有"Backload處理上傳檔案的超強能力",只需要繼承BackloadDemoController就可以。
● 在web.config中顯式配置
● 支援多種儲存位置:檔案夾、資料庫(通過Entity Framework)、雲端儲存(將來支援)
● 根據不同的請求上下文把上傳檔案儲存到不同的位置
● 根據上傳檔案的不同類型儲存到不同的位置
● 支援對圖片的裁剪,而參數可以在web.confgig中配置
● 支援對多個圖片的處理
● 支援圖片類型轉換
● 支援產生縮圖
● 支援在服務端建立唯一的檔案名稱(GUIDs)
● 還可以在原始檔案和修改檔案之間建立映射,並返回給用戶端一個友好的名稱。
● 良好的可擴充性:通過實現Badkload的介面和使用ExportAttribute特性。
● 安全性:為控制器加上驗證和授權
● 可調試:方便斷點調試發現錯誤
...

□ 安裝用戶端jQuery-File-Upload外掛程式和服務端Badkload組件,有2種方式:

1、分別安裝用戶端和服務端所需的外掛程式和組件

● 安裝服務端的Backload:

PM> Install-Package Backload

● 如果用戶端選擇使用jQuery-File-Upload外掛程式,通過如下來安裝這個用戶端檔案上傳外掛程式:

PM> Install-Package JQuery_File_Upload_Plugin

2、安裝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,發現如下問題:

1、工具--庫封裝管理員--封裝管理員控制台,被禁用

關閉VS,重新開啟就恢複。

2、報錯

未能負載檔案或程式集“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">&nbsp;</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

MVC檔案上傳-使用jQuery.FileUpload和Backload組件實現檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.