前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >原生微信小程序批量上传文件

原生微信小程序批量上传文件

作者头像
hedeqiang
修改2020-01-13 16:18:04
2.9K0
修改2020-01-13 16:18:04
举报
文章被收录于专栏:LaravelCodeLaravelCode

开发微信小程序,可能遇到最蛋疼的一个问题就是 小程序不支持批量上传,一次只能上传一个文件。想要批量上传只能递归上传。通过以下方式即可实现 “批量上传”。

index.wxml

代码语言:txt
复制
<!--index.wxml-->
<view class="container">
  <block wx:for="{{images}}" wx:key="*this">
    <view>
      <image src="{{item}}" bindtap="handleImagePreview"></image>
    </view>
  </block>
    <button bindtap="uploudImage"> 批量上传 </button>
</view>

index.js

代码语言:txt
复制
//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    images:[],
  },
  onLoad: function () {
  },
  uploudImage() {
    const that = this;
    wx.chooseImage({
      count: 9,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        console.log(res);
        that.setData({
          images: tempFilePaths,
        });
        var promise = Promise.all(tempFilePaths.map((tempFilePath, index) => {
          return new Promise(function (resolve, reject) {
            wx.uploadFile({
              url: 'uploadurl',
              filePath: tempFilePath,
              name: 'name',
              header: {},
              formData: {},
              success: function (res) {
                resolve(res.data);
              },
              fail: function (err) {
                console.log(err)
                reject(new Error('failed to upload file'));
              }
            });
          });
        }));
        promise.then(function (results) {
          console.log(results); //返回上传成功的数据
        }).catch(function (err) {
          console.log(err);
        });
      }
    })
  },
})

参考 微信小程序 官方文档

版权许可

本作品采用 知识共享署名 4.0 国际许可协议 进行许可。 转载无需与我联系,但须注明出处,注明文章来源

联系我
hedeqiang.png
hedeqiang.png
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 版权许可
  • 联系我
相关产品与服务
云开发 CloudBase
云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档