知识学习

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 5552|回复: 0

layui图片上传

[复制链接]

升级   100%

152

主题

0

回帖

752

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
752
发表于 2022-6-8 13:34:34 | 显示全部楼层 |阅读模式
  1. <!--
  2. 图片上传控件
  3. -->
  4. <div class="layui-upload">
  5.         <button type="button" class="layui-btn" name="payment_url" id="alipay_btn">上传截图</button>
  6.         <div class="layui-upload-list layui-upload-list-plus" id="alipay_photo">
  7.                  <ul class="multifyBox">
  8.                                                                                                                                            
  9.                 </ul>
  10.                  <!--<p id="uploadText_zs"></p>-->
  11.         </div>
  12. </div>                                                                                                                                                                        
  13. <!--
  14. end图片上传
  15. -->

  16. //js控制控件上传
  17. <script type="text/javascript">
  18.            /*
  19.                           lauyi 文件上传的详细用法:https://www.layui.com/doc/modules/upload.html
  20.                         */
  21.                         layui.use('upload', function(){
  22.                           var $ = layui.jquery
  23.                           ,upload = layui.upload;                          
  24.                           
  25.                           /*
  26.                            * 支付宝-转账截图/记录,可上传多张
  27.                            * */                 
  28.                          var aliPay=upload.render({
  29.                             elem: '#alipay_btn'
  30.                             ,url: '/order/imageUpload' //改成您自己的上传接口
  31.                             ,multiple: true
  32.                             ,choose: function(obj){
  33.                                     var files = obj.pushFile();
  34.                               //预读本地文件示例,不支持ie8
  35.                             obj.preview(function(index, file, result){
  36.                                       console.log(index); //得到文件索引
  37.                     console.log(file);
  38.                                 $('#alipay_photo').show().find('.multifyBox').append(
  39.                                         '<li class="mlist'+index+'">'
  40.                                             +        '<i id="removePimg'+index+'" class="removePimg"></i>'
  41.                                             +        '<img id="layui-uploadimg'+index+'" src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">'
  42.                                             +'</li>'                                                                                       
  43.                                 )
  44.                                 
  45.                                 // 删除图片
  46.                                 $("#removePimg"+index).on("click", function(event){
  47.                                         var _that=$(this);
  48.                                         //_that.next('.layui-upload-img').attr('src','')
  49.                                    // _that.parent().remove();
  50.                                    
  51.                                     delete files[index];       
  52.                                     $(".mlist"+index).remove();
  53.                                 });
  54.                                 
  55.                                 //某图片放大预览
  56.                                 $("#layui-uploadimg"+index).on('click',function () {
  57.                                     var width = $("#layui-uploadimg"+index).width();
  58.                                     var height = $("#layui-uploadimg"+index).height();
  59.                                     var scaleWH = width/height;
  60.                                     var bigH = 600;
  61.                                     var bigW = scaleWH*bigH;
  62.                                     if(bigW>900){
  63.                                         bigW = 900;
  64.                                         bigH = bigW/scaleWH;
  65.                                     }
  66.                          
  67.                                     // 放大预览图片
  68.                                     layer.open({
  69.                                         type: 1,
  70.                                         title: false,
  71.                                         closeBtn: 1,
  72.                                         shadeClose: true,
  73.                                         area: [bigW + 'px', bigH + 'px'], //宽高
  74.                                         content: "<img class='ngsPic' width='"+bigW+"' height='"+bigH+"' src=" + result + " />"
  75.                                     });
  76.                                 });                                
  77.                               });
  78.                             }
  79.                             ,done: function(res){
  80.                                     //如果上传失败
  81.                               if(res.code > 0){
  82.                                 return layer.msg('上传失败');
  83.                               }
  84.                               //得到上传后的url
  85.                                          imageUrl=res.data.url;
  86.                                          //console.log(res.data);
  87.                                     //上传完毕
  88.                                     console.log('上传完毕')
  89.                             }                          
  90.                           });
  91.                })
  92.                </script>
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|知识学习 ( 鲁ICP备20015483号 )

GMT+8, 2026-5-13 21:56 , Processed in 0.060487 second(s), 27 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表