uniapp微信小程序-上传文件(支持图片和其他文件等),一次性获得最终返回值

uniapp-微信小程序-上传文件

javascript 复制代码
<!-- 上传文件(自定义文件类型) -->
<template>
  <view class="upload-file-contarner" @click="handleUploadFile"> + </view>
</template>

<script>
export default {
  components: {},
  props: {
    //接受的附件格式
    types: {
      type: String,
      default: () => '.png,.jpg,.jpeg,.pdf,.doc,.docx,.xls,.xlsx',
    },
    //最多上传几个文件
    maxFile: {
      type: Number,
      default: 5,
    },
  },
  data() {
    return {};
  },
  methods: {
    handleUploadFile() {
      const types = this.types.split(',');
      //由于平台提供的方法只兼容app和H5,所以微信小程序使用该方法,唤起微信聊天框选择文件
      wx.chooseMessageFile({
        count: this.maxFile, //默认100
        extension: types,
        success: async (res) => {
          const files = res.tempFiles;
          const processResultFiles = async () => {
            const promise = files.map(async (v) => {
              return await this.asyncResultFiles(v);
            });
            return await Promise.all(promise);
          };
          //最终我们需要的接口返回的数据集合
          const processArr = await processResultFiles();
          this.$emit('handleUploadFile', processArr);
        },
        fail: (err) => {
          // 用户取消选择文件或选择文件失败的回调函数
          throw err;
        },
      });
    },
    //异步配合Promise.all可以一次性拿到最终的结果
    asyncResultFiles(item) {
      return new Promise(async (resolve) => {
        const result = await this.uploadFilePromise(item.path);
        const res = JSON.parse(result);
        if (res.code === '00000') {
          resolve(res.data);
        }
      });
    },
    //异步返回接口的结果
    uploadFilePromise(filePath) {
      return new Promise((resolve, reject) => {
        uni.uploadFile({
          // #ifndef H5
          url: `${process.env.VUE_APP_BACK_URL}/nqi/file/uploadTopfs`,
          // #endif
          // #ifdef H5
          url: `${process.env.VUE_APP_BASE_URL}/nqi/file/uploadTopfs`,
          // #endif
          filePath: filePath,
          name: 'file',
          header: {
            'top-token': uni.getStorageSync('top-token'),
          },
          success: (res) => {
            resolve(res.data);
          },
          fail: (err) => {
            reject(err);
          },
        });
      });
    },
  },
};
</script>
<style scoped lang="scss">
.upload-file-contarner {
  cursor: pointer;
  width: 100%;
  height: 80rpx;
  font-size: 50rpx;
  text-align: center;
  line-height: 80rpx;
  border: 1px solid $u-light-color;
  color: $u-light-color;
  margin-bottom: 20rpx;
  margin-top: 20rpx;
}
</style>
相关推荐
2501_9159184131 分钟前
iOS性能测试工具 Instruments、Keymob的使用方法 不局限 FPS
android·ios·小程序·https·uni-app·iphone·webview
苏灵凯7 小时前
智能环境监测终端全栈设计:从单片机到微信小程序,手把手搞定!
单片机·嵌入式硬件·mcu·物联网·微信小程序·小程序·蓝牙模块
nhc0888 小时前
贵阳纳海川科技有限公司・货运物流行业解决方案
科技·微信小程序·小程序·软件开发·小程序开发
admin and root8 小时前
AWS S3 对象存储攻防&云安全之OSS存储桶漏洞
微信小程序·小程序·渗透测试·云计算·aws·src·攻防演练
2501_915918418 小时前
iOS 混淆流程 提升 IPA 分析难度 实现 IPA 深度加固
android·ios·小程序·https·uni-app·iphone·webview
Ai老司机9 小时前
Notepad++ 最新版下载
notepad++·notepad·notepad 官网下载地址
前端 贾公子9 小时前
解决uni-app 输入框,键盘弹起时页面整体上移问题
前端·vue.js·uni-app
Muchen灬9 小时前
【uniapp】(5) 创建gitee仓库并推送源码
gitee·uni-app
Muchen灬9 小时前
【uniapp】(6) uniapp中使用vuex
uni-app