<link href="~/Scripts/webuploader-0.1.5/webuploader.css" rel="stylesheet" /> <script src="~/Scripts/webuploader-0.1.5/webuploader.js"></script>
htmlcss
<div id="uploader-demo"> <div id="filePicker" class="uploader-list"><img id="photo" style="width:50px;height:50px" src="/photo.jpg"></div> </div>
.uploader-list { width: 50px; } #filePicker .webuploader-pick{ background: rgba(0,0,0,0); padding:0; }
jshtml
function initUpload() { $list = $('#fileList'); // 初始化Web Uploader var uploader = WebUploader.create({ // 選完文件後,是否自動上傳。 auto: true, // swf文件路徑 swf: '~/Scripts/webuploader-0.1.5/Uploader.swf', // 文件接收服務端。 server: '/FileUp.ashx?address=Photo', // 選擇文件的按鈕。可選。 // 內部根據當前運行是建立,多是input元素,也多是flash. pick: { id: '#filePicker' }, // 只容許選擇圖片文件。 accept: { title: 'Images', extensions: 'jpg,jpeg,png', mimeTypes: 'image/jpg,image/jpeg,image/png'//不要寫'image/*' }, fileSingleSizeLimit: 1024 * 1024 * 10 //限制單個上傳圖片的大小(限制上傳單張圖片文件大小,單位是B,1M=1024000B) }); // 當有文件添加進來的時候 uploader.on('fileQueued', function (file) { //start //var $li = $( // '<div id="' + file.id + '" class="file-item thumbnail">' + // '<img>' + // '<div class="info">' + file.name + '</div>' + // '</div>' // ), //$img = $li.find('img'); // $list爲容器jQuery實例 //$list.append($li); //end //上面這些代碼是後來注掉的,爲了實現單個圖片上傳 var uploadimgWidth = $('#fileList').width(); var thumbnailWidth = uploadimgWidth; var thumbnailHeight = thumbnailWidth; // 建立縮略圖 // 若是爲非圖片文件,能夠不用調用此方法。 // thumbnailWidth x thumbnailHeight 爲 100 x 100 uploader.makeThumb(file, function (error, src) { if (error) { $('#photo').replaceWith('<span>不能預覽</span>'); return; } //$img.attr('src', src); $('#photo').attr('src', src);//爲了實現選擇圖片後顯示選中的圖片 }, thumbnailWidth, thumbnailHeight); }); // 文件上傳過程當中建立進度條實時顯示。 uploader.on('uploadProgress', function (file, percentage) { var $li = $('#' + file.id), $percent = $li.find('.progress span'); // 避免重複建立 if (!$percent.length) { $percent = $('<p class="progress"><span></span></p>') .appendTo($li) .find('span'); } $percent.css('width', percentage * 100 + '%'); }); // 文件上傳成功,給item添加成功class, 用樣式標記上傳成功。 uploader.on('uploadSuccess', function (file,response) {//respoonse是後臺返回的內容 $('#' + file.id).addClass('upload-state-done'); }); // 文件上傳失敗,顯示上傳出錯。 uploader.on('uploadError', function (file) { var $li = $('#' + file.id), $error = $li.find('div.error'); // 避免重複建立 if (!$error.length) { $error = $('<div class="error"></div>').appendTo($li); } $error.text('上傳失敗'); }); // 完成上傳完了,成功或者失敗,先刪除進度條。 uploader.on('uploadComplete', function (file) { $('#' + file.id).find('.progress').remove(); }); }
後臺(通常處理程序)web
context.Response.ContentType = "text/html";
if (context.Request.Files.Count <= 0) { return; } //建立文件夾 string name = context.Request["address"]; string dicPath = context.Server.MapPath("/UploadFiles/" + name + "/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/"); if (!Directory.Exists(dicPath)) { Directory.CreateDirectory(dicPath); } //保存圖片 HttpPostedFile img = context.Request.Files[0]; string ext = Path.GetExtension(img.FileName); string fileName = Guid.NewGuid().ToString() + ext; string filePath = Path.Combine(dicPath, fileName); img.SaveAs(filePath);
context.Response.Write(urlPrev+fileName);
public IHostingEnvironment _hostingEnvironment{get;set;} public ActionResult UploadFile() { if (Request.Form.Files.Count <= 0) { return Content("請選擇圖片"); } string name = Request.Query["address"]; string imgPath="\\UploadFiles\\" + name + "\\" + DateTime.Now.Year + "\\" + DateTime.Now.Month + "\\" + DateTime.Now.Day + "\\"; string dicPath = _hostingEnvironment.WebRootPath+imgPath; if (!Directory.Exists(dicPath)) { Directory.CreateDirectory(dicPath); } var img = Request.Form.Files[0]; if(img==null){ return Content("上傳失敗"); } string ext = Path.GetExtension(img.FileName); //判斷後綴是不是圖片 const string fileFilt = ".jpg|.jpeg|.png|"; //判斷後綴是不是圖片 if (ext == null) { return Content("上傳的文件沒有後綴" ); } if (fileFilt.IndexOf(ext.ToLower(), StringComparison.Ordinal) <= -1) { return Content("上傳的文件不是圖片"); } string fileName = Guid.NewGuid().ToString() + ext; string filePath = Path.Combine(dicPath, fileName); using(FileStream fs = System.IO.File.Create(filePath)){ img.CopyTo(fs); fs.Flush(); } return Content(imgPath+fileName); }