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>
相关推荐
蓝帆傲亦5 分钟前
支付宝小程序性能暴增秘籍:UniApp项目极限优化全攻略
小程序·uni-app
2501_933907216 小时前
深圳本凡科技专业企业APP开发,助力手机应用创新优化
科技·微信小程序·小程序
java1234_小锋8 小时前
分享一套优质的微信小程序校园志愿者系统(SpringBoot后端+Vue3管理端)
微信小程序·小程序·校园志愿者
2501_9160088910 小时前
深入解析iOS机审4.3原理与混淆实战方法
android·java·开发语言·ios·小程序·uni-app·iphone
QT.qtqtqtqtqt11 小时前
uni-app小程序前端开发笔记(更新中)
前端·笔记·小程序·uni-app
喵喵虫21 小时前
uniapp修改封装组件失败 styleIsolation
uni-app
游戏开发爱好者81 天前
日常开发与测试的 App 测试方法、查看设备状态、实时日志、应用数据
android·ios·小程序·https·uni-app·iphone·webview
2501_915106321 天前
app 上架过程,安装包准备、证书与描述文件管理、安装测试、上传
android·ios·小程序·https·uni-app·iphone·webview
2501_915106321 天前
使用 Sniffmaster TCP 抓包和 Wireshark 网络分析
网络协议·tcp/ip·ios·小程序·uni-app·wireshark·iphone
宠友信息1 天前
2025社交+IM及时通讯社区APP仿小红书小程序
java·spring boot·小程序·uni-app·web app