Asp.Net Mvc 使用WebUploader 多圖片上傳

1. 下載WebUploaderjavascript

2. 將下載到的壓縮包裏面的文件複製到本身的項目中css

3. 添加引用html

1 <!--引入Jquery-->
2 <script src="~/Script/jquery-1.8.2.min.js"></script>
3 <!--引入Css-->
4 <link href="~/CSS/webuploader.css" rel="stylesheet" />
5 <!--引入Js-->
6 <script src="~/Script/webuploader.js"></script>

4.準備一個放圖片的容器和一個上傳按鈕java

<div id="fileList"></div> <!--這是存放圖片的容器-->
<div class="cp_img_jia" id="filePicker">
<input type="button" id="ctlBtn" value="上傳" />
</div> <!--這是上傳按鈕-->

5.建立Web Uploader實例並監聽事件jquery

  1  1 <script type="text/javascript">
  2   2 
  3   3     var applicationPath = window.applicationPath === "" ? "" : window.applicationPath || "../../";
  4   4     $(function () {
  5   5         var $ = jQuery,
  6   6         $list = $('#fileList'),
  7   7         // 優化retina, 在retina下這個值是2
  8   8         ratio = window.devicePixelRatio || 1,
  9   9         // 縮略圖大小
 10  10         thumbnailWidth = 90 * ratio,
 11  11         thumbnailHeight = 90 * ratio,
 12  12         // Web Uploader實例
 13  13         uploader;
 14  14         uploader = WebUploader.create({
 15  15             // 選完文件後,是否自動上傳。
 16  16             auto: false,
 17  17 
 18  18             // swf文件路徑
 19  19             swf: applicationPath + '/Script/Uploader.swf',
 20  20 
 21  21             // 文件接收服務端。
 22  22             server: applicationPath + '/Home/UpLoadProcess',
 23  23 
 24  24             // 選擇文件的按鈕。可選。
 25  25             // 內部根據當前運行是建立,多是input元素,也多是flash.
 26  26             pick: '#filePicker',
 27  27 
 28  28             //只容許選擇圖片
 29  29             accept: {
 30  30                 title: 'Images',
 31  31                 extensions: 'gif,jpg,jpeg,bmp,png',
 32  32                 mimeTypes: 'image/*'
 33  33             }
 34  34         });
 35  35        
 36  36         // 當有文件添加進來的時候
 37  37         uploader.on('fileQueued', function (file) {
 38  38             var $li = $(
 39  39                     '<div id="' + file.id + '" class="cp_img">' +
 40  40                         '<img>' +
 41  41                     '<div class="cp_img_jian"></div></div>'
 42  42                     ),
 43  43                 $img = $li.find('img');
 44  44 
 45  45 
 46  46             // $list爲容器jQuery實例
 47  47             $list.append($li);
 48  48 
 49  49             // 建立縮略圖
 50  50             // 若是爲非圖片文件,能夠不用調用此方法。
 51  51             // thumbnailWidth x thumbnailHeight 爲 100 x 100
 52  52             uploader.makeThumb(file, function (error, src) {
 53  53                 if (error) {
 54  54                     $img.replaceWith('<span>不能預覽</span>');
 55  55                     return;
 56  56                 }
 57  57 
 58  58                 $img.attr('src', src);
 59  59             }, thumbnailWidth, thumbnailHeight);
 60  60         });
 61  61 
 62  62         // 文件上傳過程當中建立進度條實時顯示。
 63  63         uploader.on('uploadProgress', function (file, percentage) {
 64  64             var $li = $('#' + file.id),
 65  65                 $percent = $li.find('.progress span');
 66  66 
 67  67             // 避免重複建立
 68  68             if (!$percent.length) {
 69  69                 $percent = $('<p class="progress"><span></span></p>')
 70  70                         .appendTo($li)
 71  71                         .find('span');
 72  72             }
 73  73 
 74  74             $percent.css('width', percentage * 100 + '%');
 75  75         });
 76  76 
 77  77         // 文件上傳成功,給item添加成功class, 用樣式標記上傳成功。
 78  78         uploader.on('uploadSuccess', function (file, response) {
 79  79             
 80  80             $('#' + file.id).addClass('upload-state-done');
 81  81         });
 82  82 
 83  83         // 文件上傳失敗,顯示上傳出錯。
 84  84         uploader.on('uploadError', function (file) {
 85  85             var $li = $('#' + file.id),
 86  86                 $error = $li.find('div.error');
 87  87 
 88  88             // 避免重複建立
 89  89             if (!$error.length) {
 90  90                 $error = $('<div class="error"></div>').appendTo($li);
 91  91             }
 92  92 
 93  93             $error.text('上傳失敗');
 94  94         });
 95  95 
 96  96         // 完成上傳完了,成功或者失敗,先刪除進度條。
 97  97         uploader.on('uploadComplete', function (file) {
 98  98             $('#' + file.id).find('.progress').remove();
 99  99         });
100 100 
101 101         //全部文件上傳完畢
102 102         uploader.on("uploadFinished", function ()
103 103         {
104 104            //提交表單
105 105 
106 106         });
107 107 
108 108         //開始上傳
109 109         $("#ctlBtn").click(function () {
110 110             uploader.upload();
111 111 
112 112         });
113 113 
114 114         //顯示刪除按鈕
115 115         $(".cp_img").live("mouseover", function ()
116 116         {
117 117             $(this).children(".cp_img_jian").css('display', 'block');
118 118 
119 119         });
120 120         //隱藏刪除按鈕
121 121         $(".cp_img").live("mouseout", function () {
122 122             $(this).children(".cp_img_jian").css('display', 'none');
123 123 
124 124         });
125 125         //執行刪除方法
126 126         $list.on("click", ".cp_img_jian", function ()
127 127         {
128 128             var Id = $(this).parent().attr("id");
129 129             uploader.removeFile(uploader.getFile(Id,true));
130 130             $(this).parent().remove();
131 131         });
132 132       
133 133     });
134 134 
135 135 
136 136 </script>

6 在Controller裏新建一個Action用於保存圖片並返回圖片路徑(這方法是 eflay 前輩博客上說的)git

1  public ActionResult UpLoadProcess(string id, string name, string type, string lastModifiedDate, int size, HttpPostedFileBase file)
 2         {
 3             string filePathName = string.Empty;
 4 
 5             string localPath = Path.Combine(HttpRuntime.AppDomainAppPath, "Upload");
 6             if (Request.Files.Count == 0)
 7             {
 8                 return Json(new { jsonrpc = 2.0, error = new { code = 102, message = "保存失敗" }, id = "id" });
 9             }
10 
11             string ex = Path.GetExtension(file.FileName);
12             filePathName = Guid.NewGuid().ToString("N") + ex;
13             if (!System.IO.Directory.Exists(localPath))
14             {
15                 System.IO.Directory.CreateDirectory(localPath);
16             }
17             file.SaveAs(Path.Combine(localPath, filePathName));
18 
19             return Json(new
20             {
21                 jsonrpc = "2.0",
22                 id = id,
23                 filePath = "/Upload/" + filePathName
24             });
25         
26         }

再次謝謝原著。github

相關文章
相關標籤/搜索