登录  /  注册

关于Asp.net MVC如何利用swupload来实现多图片上传的示例代码分享

黄舟
发布: 2017-07-17 11:24:05
原创
1346人浏览过

这篇文章主要为大家详细介绍了asp.net mvc使用swupload实现多图片上传功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了swupload实现多图片上传的具体代码,供大家参考,具体内容如下

1. 下载WebUploader

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

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

<p id="fileList"></p> <!--这是存放图片的容器-->
<p class="cp_img_jia" id="filePicker"></p> <!--这是上传按钮-->
登录后复制

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

<script type="text/javascript">

  var applicationPath = window.applicationPath === "" ? "" : window.applicationPath || "../../";
  $(function () {
    var $ = jQuery,
    $list = $(&#39;#fileList&#39;),
    // 优化retina, 在retina下这个值是2
    ratio = window.devicePixelRatio || 1,
    // 缩略图大小
    thumbnailWidth = 90 * ratio,
    thumbnailHeight = 90 * ratio,
    // Web Uploader实例
    uploader;
    uploader = WebUploader.create({
      // 选完文件后,是否自动上传。
      auto: false,

      // swf文件路径
      swf: applicationPath + &#39;/Script/Uploader.swf&#39;,

      // 文件接收服务端。
      server: applicationPath + &#39;/Home/UpLoadProcess&#39;,

      // 选择文件的按钮。可选。
      // 内部根据当前运行是创建,可能是input元素,也可能是flash.
      pick: &#39;#filePicker&#39;,

      //只允许选择图片
      accept: {
        title: &#39;Images&#39;,
        extensions: &#39;gif,jpg,jpeg,bmp,png&#39;,
        mimeTypes: &#39;image/*&#39;
      }
    });
    
    // 当有文件添加进来的时候
    uploader.on(&#39;fileQueued&#39;, function (file) {
      var $li = $(
          &#39;<p id="&#39; + file.id + &#39;" class="cp_img">&#39; +
            &#39;<img  alt="关于Asp.net MVC如何利用swupload来实现多图片上传的示例代码分享" >&#39; +
          &#39;<p class="cp_img_jian"></p></p>&#39;
          ),
        $img = $li.find(&#39;img&#39;);


      // $list为容器jQuery实例
      $list.append($li);

      // 创建缩略图
      // 如果为非图片文件,可以不用调用此方法。
      // thumbnailWidth x thumbnailHeight 为 100 x 100
      uploader.makeThumb(file, function (error, src) {
        if (error) {
          $img.replaceWith(&#39;<span>不能预览</span>&#39;);
          return;
        }

        $img.attr(&#39;src&#39;, src);
      }, thumbnailWidth, thumbnailHeight);
    });

    // 文件上传过程中创建进度条实时显示。
    uploader.on(&#39;uploadProgress&#39;, function (file, percentage) {
      var $li = $(&#39;#&#39; + file.id),
        $percent = $li.find(&#39;.progress span&#39;);

      // 避免重复创建
      if (!$percent.length) {
        $percent = $(&#39;<p class="progress"><span></span></p>&#39;)
            .appendTo($li)
            .find(&#39;span&#39;);
      }

      $percent.css(&#39;width&#39;, percentage * 100 + &#39;%&#39;);
    });

    // 文件上传成功,给item添加成功class, 用样式标记上传成功。
    uploader.on(&#39;uploadSuccess&#39;, function (file, response) {
      
      $(&#39;#&#39; + file.id).addClass(&#39;upload-state-done&#39;);
    });

    // 文件上传失败,显示上传出错。
    uploader.on(&#39;uploadError&#39;, function (file) {
      var $li = $(&#39;#&#39; + file.id),
        $error = $li.find(&#39;p.error&#39;);

      // 避免重复创建
      if (!$error.length) {
        $error = $(&#39;<p class="error"></p>&#39;).appendTo($li);
      }

      $error.text(&#39;上传失败&#39;);
    });

    // 完成上传完了,成功或者失败,先删除进度条。
    uploader.on(&#39;uploadComplete&#39;, function (file) {
      $(&#39;#&#39; + file.id).find(&#39;.progress&#39;).remove();
    });

    //所有文件上传完毕
    uploader.on("uploadFinished", function ()
    {
      //提交表单

    });

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

    });

    //显示删除按钮
    $(".cp_img").live("mouseover", function ()
    {
      $(this).children(".cp_img_jian").css(&#39;display&#39;, &#39;block&#39;);

    });
    //隐藏删除按钮
    $(".cp_img").live("mouseout", function () {
      $(this).children(".cp_img_jian").css(&#39;display&#39;, &#39;none&#39;);

    });
    //执行删除方法
    $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
      });
    
    }
登录后复制

这样就大功告成了。

以上就是关于Asp.net MVC如何利用swupload来实现多图片上传的示例代码分享的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号