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

作者:In 时间:2024-04-29 14:06:10 

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

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;
}

示例图

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

来源:https://blog.csdn.net/heavenz19/article/details/109813539

标签:微信小程序,上传
0
投稿

猜你喜欢

  • SQL Server误区30日谈 第11天 镜像在检测到故障后瞬间就能故障转移

    2024-01-22 13:13:00
  • PHP源码之 ext/mysql扩展部分

    2023-07-21 18:24:20
  • pyenv命令管理多个Python版本

    2023-10-18 15:44:38
  • 如何使用PyCharm引入需要使用的包的方法

    2021-12-22 07:34:56
  • JavaScript高级程序设计 阅读笔记(十五) 浏览器中的JavaScript

    2024-04-10 10:57:11
  • Javascript 实现的数独解题算法网页实例

    2024-02-26 15:26:16
  • 解决Python2.7读写文件中的中文乱码问题

    2023-05-16 11:23:51
  • python3利用tcp实现文件夹远程传输

    2023-09-03 07:44:38
  • 基于jQuery实现的立体文字渐变效果

    2009-05-18 19:15:00
  • python实例小练习之Turtle绘制南方的雪花

    2023-11-08 11:48:29
  • 使用 prometheus python 库编写自定义指标的方法(完整代码)

    2021-03-03 06:03:58
  • Python多进程模式实现多核CPU并行计算

    2022-12-01 21:26:20
  • Python常用内置函数总结

    2022-06-06 06:02:43
  • 微软建议的ASP性能优化28条守则(6)

    2008-02-29 11:43:00
  • PL/SQL DEVELOPER 使用的一些技巧

    2024-01-22 00:49:29
  • Apache+php+mysql在windows下的安装与配置(图文)第1/2页

    2024-05-11 09:25:34
  • 怎么用FSO生成一个UNICODE格式的文本文件

    2009-05-26 15:40:00
  • 详解MySQL中的缓冲池(buffer pool)

    2024-01-26 01:05:53
  • Python 3中print函数的使用方法总结

    2021-12-21 00:37:18
  • “Unable to read local eventlog (reason:事件日志文件已在读取时间更改)”解决办法

    2009-08-27 13:12:00
  • asp之家 网络编程 m.aspxhome.com