一:下载miniprogram-file-uploader

npm i miniprogram-file-uploader

二:使用miniprogram-file-uploader

import Uploader from "miniprogram-file-uploader";
methods: {

async uploadVideo(filePath, fileSize, fileName) {
      if (Uploader.isSupport()) {
        const uploader = new Uploader({
          fileName: fileName,
          tempFilePath: filePath,
          totalSize: fileSize,
          chunkSize: 1024 * 1024,//一片1m
          uploadUrl: , // 你的文件上传接口URL
          mergeUrl: , // 文件合并接口URL

          header: {
            Authorization: "Bearer " + uni.getStorageSync("token") || "",//看后端要求,uploadUrl可加,mergeurl加不了
          },
        });

        uploader.on("progress", (res) => {
          const progress = Math.round(res.progress);
          uni.showLoading({
            title: `上传中 ${progress}%`,
            mask: true,
          });
          console.log("上传进度:", res.progress);
        });

        uploader.on("success", async (res) => {
          console.log("上传成功", res);
          uni.hideLoading();
          await this.handleUploadSuccess(res);
        });

        uploader.on("fail", (errorData) => {
          uni.hideLoading();
          uni.showToast({
            title: errorData.errMsg || "上传失败",
            icon: "none",
          });
          console.log("上传失败:", errorData);
        });

        uploader.upload();
      } else {
        wx.showToast({ title: "当前版本不支持", icon: "none" });
      }
    },
}
Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐