1. 下載WebUploader 2. 將下載到的壓縮包裡面的文件複製到自己的項目中 3. 添加引用 4.準備一個放圖片的容器和一個上傳按鈕 5.創建Web Uploader實例並監聽事件 6 在Controller里新建一個Action用於保存圖片並返回圖片路徑(這方法是 eflay 前輩博客上說 ...
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"></div> <!--這是上傳按鈕-->
5.創建Web Uploader實例並監聽事件

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

這樣就大功告成了。由於是第一次寫博客,裡面如果有寫的不詳細或不對的地方,歡迎大家指點。希望能和大家一起進步。
Demo下載地址http://pan.baidu.com/s/1hqqvB0o