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

本文实例为大家分享了微信小程序实现多文件或者图片上传的具体代码,供大家参考,具体内容如下

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: {
    isHidden: true,//原始添加
    url: [],//上传文件路径
    type: '',//上传文件类型
    uploaderList: [],//上传文件数组
    showModal: false,
  },
// // 上传图片
  upImg() {
    var that = this

    wx.chooseMessageFile({
      count: 1,
      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({
                  type: type,
                  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,
      isHidden: true
    })
  },

css

.list1 {
  width: 670rpx;
  margin: 0 auto;
}

.tops {
  display: flex;
  margin-bottom: 22rpx;
}

.left1 {
  margin-left: 12rpx;
  line-height: 44rpx;
  text-align: left;
}

.listImg1 {
  width: 44rpx;
  height: 44rpx;
  margin-left: 24rpx;
}

.textarea1 {
  width: 670rpx;
  height: 250rpx;
  line-height: 40rpx;
  border-radius: 44rpx;
  background-color: rgba(255, 255, 255, 1);
  color: rgba(16, 16, 16, 1);
  font-size: 14px;
  text-align: left;
  border: 1px solid rgba(240, 240, 240, 1);
  margin:0 auto;
  padding: 20rpx 40rpx;
  box-sizing: border-box;
  margin-bottom: 40rpx;
}
.jia{
  width: 140rpx;
  height: 140rpx;
  margin-top: 40rpx;
}
.jia image{
  width: 140rpx;
  height: 140rpx;
}
.cell {
  width: 100%;
  overflow: hidden;
}
.jinListImg {
  width: 140rpx;
  height: 140rpx;
  border-radius: 24rpx;
}

.close1 {
  width: 40rpx;
  height: 40rpx;
  position: absolute;
  margin-left: 100rpx;
}

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

示例图

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 微信小程序上传图片到服务器实例代码

    上传图片到服务器: 1.先在前端写一个选择图片的区域来触发wx.chooseImage接口并用wx.setStorage接口把图片路径存起来. -wxml <view class="shangchuan" bindtap="choose"> <image style="width:100%;height:100%;" src="{{tempFilePaths}}"></image> <

  • 微信小程序实现上传图片功能

    微信小程序图片上传,供大家参考,具体内容如下 先来看一下微信小程序的api 来看一下页面效果 查看大图 wxml文件代码: <view class="weui-cell"> <view class="weui-cell__bd"> <view class="weui-uploader"> <view class="weui-uploader__hd"> <view clas

  • 微信小程序多张图片上传功能

    微信小程序上传图片每次只能上传一张,所有很多朋友就会问想要多张图片上传怎么办? 首先,我们来看一看wx.chooseImage(object)和wx.uploadFile(OBJECT)这两个个api 示例代码是这样的: wx.chooseImage({ success: function(res) { var tempFilePaths = res.tempFilePaths wx.uploadFile({ url: 'http://example.weixin.qq.com/upload',

  • 微信小程序开发(二)图片上传+服务端接收详解

    这次介绍下小程序当中常用的图片上传. 前几天做了图片上传功能,被坑了一下.接下来我们来看一下微信的上传api. 这里的filePath就是图片的存储路径,类型居然是个String,也就是 只能每次传一张图片,我以前的接口都是接收一个array,我本人又是一个半吊子的PHP,只能自己去改接收图片的接口. 看一下页面效果图 一个很常见的修改头像效果,选择图片(拍照),然后上传. 下面就是贴代码了 首先是小程序的wxml代码 <view class="xd-container">

  • 微信小程序-拍照或选择图片并上传文件

    微信小程序-拍照或选择图片并上传文件 调用拍照API:https://mp.weixin.qq.com/debug/wxadoc/dev/api/media-picture.html?t=20161222#wxchooseimageobject 上传文件API:https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-file.html 主要js代码: choice: function () { var that = this wx.choose

  • 微信小程序实现pdf、word等格式文件上传的方法

    目前微信只支持从聊天记录里面获取文件 一.前言 目前微信提供了一个接口 wx.chooseMessageFile 它能让用户从聊天记录里面选择一个或者多个文件,然后返回它的一些信息,列入文件的path地址,文件名,文件的大小等. 获取这些信息再结合微信的上传接口wx.uploadFile,即可实现文件上传. 二.具体实现 首先需要一个按钮来调用wx.chooseMessageFile. wx.chooseMessageFile({ count: 1, //能选择文件的数量 type: 'file

  • 微信小程序实现上传word、txt、Excel、PPT等文件功能

    正文: 目前小程序没有能实现此功能的 API 所以我这里通过使用 web-view 实现: 实现流程: 1. 在小程序后台配置业务域名 2. 在服务器写一个html,实现表单上传文件 3.后端php接收文件并存到一个服务器文件夹,把文件名存到数据库以后检索用 4.在微信小程序创建一个页面,里面使用web-view达到上传文件的目的: 效果图: 具体实现: 1. 在小程序后台配置业务域名 2. 在服务器写一个html,实现表单上传文件 index.html文件 <!DOCTYPE html> &

  • 微信小程序实现文件、图片上传功能

    本文实例为大家分享了微信小程序实现文件图片上传的具体代码,供大家参考,具体内容如下 在我看来微信小程序的功能挺强大的,提供了很多API让你直接使用. 这里我说一下微信小程序如何实现图片的上传 1.在微信公众号平台设置uploadFile合法域名 点击设置-开发设置,可以看到服务器域名,点击修改,设置一下你的uploadFile合法域名. 否则会出现以下错误. 2.使用wx.chooseImage和wx.uploadFile实现图片上传 代码如下 wx.chooseImage({ count: 1

  • 微信小程序实现上传多个文件 超过10个

    本文实例为大家分享了微信小程序实现上传多个文件超过10个的具体代码,供大家参考,具体内容如下 [小程序笔记]wx.uploadFile(OBJECT) 先说说遇到的问题: 小程序可通过wx.uploadFile(OBJECT)接口上传手机文件至服务器,但是在文档中关于请求中有这么一段说明: request.uploadFile.downloadFile 的最大并发限制是 10 个 意思就是这三个接口请求并发数不能超过10个,否则报以下错误 uploadFile:fail exceed max u

  • 微信小程序上传图片功能(附后端代码)

    几乎每个程序都需要用到图片,在小程序中我们可以通过image组件显示图片. 当然小程序也是可以上传图片的,微信小程序文档也写的很清楚. 上传图片 首先选择图片 通过wx.chooseImage(OBJECT)实现 官方示例代码 wx.chooseImage({ count: 1, // 默认9 sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], // 可以指定来

随机推荐