溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

在Asp.net的MVC中如何利用swupload實現(xiàn)多圖片上傳功能的案例

發(fā)布時間:2020-10-24 15:31:46 來源:億速云 閱讀:193 作者:小新 欄目:編程語言

這篇文章將為大家詳細(xì)講解有關(guān)在Asp.net的MVC中如何利用swupload實現(xiàn)多圖片上傳功能的案例,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

具體內(nèi)容如下

1. 下載WebUploader

2. 將下載到的壓縮包里面的文件復(fù)制到自己的項目中

3. 添加引用

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

4.準(zhǔn)備一個放圖片的容器和一個上傳按鈕

<p id="fileList"></p> <!--這是存放圖片的容器-->
<p class="cp_img_jia" id="filePicker"></p> <!--這是上傳按鈕-->

5.創(chuàng)建Web Uploader實例并監(jiān)聽事件

<script type="text/javascript">

 var applicationPath = window.applicationPath === "" ? "" : window.applicationPath || "../../";
 $(function () {
  var $ = jQuery,
  $list = $('#fileList'),
  // 優(yōu)化retina, 在retina下這個值是2
  ratio = window.devicePixelRatio || 1,
  // 縮略圖大小
  thumbnailWidth = 90 * ratio,
  thumbnailHeight = 90 * ratio,
  // Web Uploader實例
  uploader;
  uploader = WebUploader.create({
   // 選完文件后,是否自動上傳。
   auto: false,

   // swf文件路徑
   swf: applicationPath + '/Script/Uploader.swf',

   // 文件接收服務(wù)端。
   server: applicationPath + '/Home/UpLoadProcess',

   // 選擇文件的按鈕??蛇x。
   // 內(nèi)部根據(jù)當(dāng)前運(yùn)行是創(chuàng)建,可能是input元素,也可能是flash.
   pick: '#filePicker',

   //只允許選擇圖片
   accept: {
    title: 'Images',
    extensions: 'gif,jpg,jpeg,bmp,png',
    mimeTypes: 'image/*'
   }
  });
  
  // 當(dāng)有文件添加進(jìn)來的時候
  uploader.on('fileQueued', function (file) {
   var $li = $(
     '<p id="' + file.id + '" class="cp_img">' +
      '<img>' +
     '<p class="cp_img_jian"></p></p>'
     ),
    $img = $li.find('img');


   // $list為容器jQuery實例
   $list.append($li);

   // 創(chuàng)建縮略圖
   // 如果為非圖片文件,可以不用調(diào)用此方法。
   // thumbnailWidth x thumbnailHeight 為 100 x 100
   uploader.makeThumb(file, function (error, src) {
    if (error) {
     $img.replaceWith('<span>不能預(yù)覽</span>');
     return;
    }

    $img.attr('src', src);
   }, thumbnailWidth, thumbnailHeight);
  });

  // 文件上傳過程中創(chuàng)建進(jìn)度條實時顯示。
  uploader.on('uploadProgress', function (file, percentage) {
   var $li = $('#' + file.id),
    $percent = $li.find('.progress span');

   // 避免重復(fù)創(chuàng)建
   if (!$percent.length) {
    $percent = $('<p class="progress"><span></span></p>')
      .appendTo($li)
      .find('span');
   }

   $percent.css('width', percentage * 100 + '%');
  });

  // 文件上傳成功,給item添加成功class, 用樣式標(biāo)記上傳成功。
  uploader.on('uploadSuccess', function (file, response) {
   
   $('#' + file.id).addClass('upload-state-done');
  });

  // 文件上傳失敗,顯示上傳出錯。
  uploader.on('uploadError', function (file) {
   var $li = $('#' + file.id),
    $error = $li.find('p.error');

   // 避免重復(fù)創(chuàng)建
   if (!$error.length) {
    $error = $('<p class="error"></p>').appendTo($li);
   }

   $error.text('上傳失敗');
  });

  // 完成上傳完了,成功或者失敗,先刪除進(jìn)度條。
  uploader.on('uploadComplete', function (file) {
   $('#' + file.id).find('.progress').remove();
  });

  //所有文件上傳完畢
  uploader.on("uploadFinished", function ()
  {
   //提交表單

  });

  //開始上傳
  $("#ctlBtn").click(function () {
   uploader.upload();

  });

  //顯示刪除按鈕
  $(".cp_img").live("mouseover", function ()
  {
   $(this).children(".cp_img_jian").css('display', 'block');

  });
  //隱藏刪除按鈕
  $(".cp_img").live("mouseout", function () {
   $(this).children(".cp_img_jian").css('display', 'none');

  });
  //執(zhí)行刪除方法
  $list.on("click", ".cp_img_jian", function ()
  {
   var Id = $(this).parent().attr("id");
   uploader.removeFile(uploader.getFile(Id,true));
   $(this).parent().remove();
  });
  
 });


</script>

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

public ActionResult UpLoadProcess(string id, string name, string type, string lastModifiedDate, int size, HttpPostedFileBase file)
  {
   string filePathName = string.Empty;

   string localPath = Path.Combine(HttpRuntime.AppDomainAppPath, "Upload");
   if (Request.Files.Count == 0)
   {
    return Json(new { jsonrpc = 2.0, error = new { code = 102, message = "保存失敗" }, id = "id" });
   }

   string ex = Path.GetExtension(file.FileName);
   filePathName = Guid.NewGuid().ToString("N") + ex;
   if (!System.IO.Directory.Exists(localPath))
   {
    System.IO.Directory.CreateDirectory(localPath);
   }
   file.SaveAs(Path.Combine(localPath, filePathName));

   return Json(new
   {
    jsonrpc = "2.0",
    id = id,
    filePath = "/Upload/" + filePathName
   });
  
  }

這樣就大功告成了。

關(guān)于在Asp.net的MVC中如何利用swupload實現(xiàn)多圖片上傳功能的案例就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI