温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

微信小程序怎么实现多文件或图片上传

发布时间:2022-07-07 13:38:41 阅读:1165 作者:iii 栏目:开发技术
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

本文小编为大家详细介绍“微信小程序怎么实现多文件或图片上传”,内容详细,步骤清晰,细节处理妥当,希望这篇“微信小程序怎么实现多文件或图片上传”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

html

<view class="list1">
    <view class="fonts">上传事件相关资料或文件(jpg/pdf/word)<text class="xuan">(选填)</text> </view>
    <view class="cell">
        <view class='jinList' wx:for="{{uploaderList}}" wx:key="index">
            <image class="close1" bindtap="close" data-index="{{index}}" src="../../images/close1.png"></image>
        <image class="jinListImg" wx:if="{{type != 'file'}}" bindtap='showImg' data-index="{{index}}" src='{{item}}'></image>
            <image class="jinListImg" src="../../images/img.png" wx:if="{{type == 'file'}}" mode="aspectFill"></image>
    </view>
    <view class="jia jia1" bindtap="upImg">
            <image src="../../images/jia.png"  wx:if="{{isHidden}}"></image>
        </view>
    </view>
</view>

js

data: {
    isHiddentrue,//原始添加
    url: [],//上传文件路径
    type'',//上传文件类型
    uploaderList: [],//上传文件数组
    showModalfalse,
  },
// // 上传图片
  upImg() {
    var that = this

    wx.chooseMessageFile({
      count1,
      type'all',
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        //  console.log('vvvvvvvv',res.tempFiles)

        const tempFilePaths = res.tempFiles[0].path
        const type = res.tempFiles[0].type
        wx.uploadFile({
          url: app.globalData.urlSrc + '/api/chuan/index',
          filePath: tempFilePaths,
          name'file',
          success(res) {
            const datas = JSON.parse(res.data)
            console.log('上传文件', datas)
            var status = datas.status
            that.data.list
            if (status == 1) {
              var upFiles = datas.data
              if (upFiles != '') {
                let tempFile = tempFilePaths;
                let uploaderList = that.data.uploaderList.concat(tempFile);//返回页面的图片数据
                that.data.url = that.data.url.concat(upFiles);//传给后台的图片数据
                that.setData({
                  typetype,
                  uploaderList: uploaderList
                })
              }
            }
          }
        })

      }
    })

  },

  // 删除上传
  close(e) {
    var that = this
    var nowList = [];//新数据
    var uploaderList = that.data.uploaderList;//原数据

    for (let i = 0; i < uploaderList.length; i++) {
      if (i == e.currentTarget.dataset.index) {
        continue;
      } else {
        nowList.push(uploaderList[i])
      }
    }
    that.setData({
      uploaderList: nowList,
      isHiddentrue
    })
  },

css

.list1 {
  width670rpx;
  margin0 auto;
}

.tops {
  display: flex;
  margin-bottom22rpx;
}

.left1 {
  margin-left12rpx;
  line-height44rpx;
  text-align: left;
}

.listImg1 {
  width44rpx;
  height44rpx;
  margin-left24rpx;
}

.textarea1 {
  width670rpx;
  height250rpx;
  line-height40rpx;
  border-radius44rpx;
  background-colorrgba(2552552551);
  colorrgba(1616161);
  font-size14px;
  text-align: left;
  border1px solid rgba(2402402401);
  margin:0 auto;
  padding20rpx 40rpx;
  box-sizing: border-box;
  margin-bottom40rpx;
}
.jia{
  width140rpx;
  height140rpx;
  margin-top40rpx;
}
.jia image{
  width140rpx;
  height140rpx;
}
.cell {
  width100%;
  overflow: hidden;
}
.jinListImg {
  width140rpx;
  height140rpx;
  border-radius24rpx;
}

.close1 {
  width40rpx;
  height40rpx;
  position: absolute;
  margin-left100rpx;
}

.jia1 {
  float: left;
  position: relative;
}

示例图

微信小程序怎么实现多文件或图片上传

读到这里,这篇“微信小程序怎么实现多文件或图片上传”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×