Asp.Net Mvc 使用WebUploader 多图片上传

1. 下载WebUploader

2. 将下载到的压缩包里面的文件复制到自己的项目中

3. 添加引用

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.准备一个放图片的容器和一个上传按钮

<div id="fileList"></div> <!--这是存放图片的容器-->
<div class="cp_img_jia" id="filePicker">
<input type="button" id="ctlBtn" value="上传" />
</div> <!--这是上传按钮-->

5.创建Web Uploader实例并监听事件

  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 前辈博客上说的)

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         }

再次谢谢原著。

原文地址:https://www.cnblogs.com/longshanshan/p/6144898.html