vue element upload取消上传后终止请求

当文件过大上传时,需要很长时间,用户如果点击取消上传,但是接口还在pending,所以需要在点击取消上传的时候终止请求

其实问题是因为我们表单涉及到上传是:需要上传的时候先调上传的api,后端返回一个当前上传文件的Id,再在表单提交的时候携带这个fileId提交过去

好处就是:不需要在表单的上传的时候new formData,在append(我个人感觉好处是这个)

思路

我是一个表单有多个upload文件。所以formRef定义了两个fileId和fileList

javascript 复制代码
import { reactive } from "vue";
const formModel = reactive({
  name: "",
  fileId: "",
  modelConfigFileId: "",
  fileList: [],
  fileConfigList: []
});

上传使用的方法是upload的http-request api,取消上传使用的是on-removeapi

template代码

javascript 复制代码
<el-form
ref="ruleFormRef"
:model="formModel"
>
		<el-form-item label="Model name" prop="name">
            <el-input v-model="formModel.name" />
        </el-form-item>
        <el-form-item label="File " prop="fileList">
            <el-upload
              v-model:file-list="formModel.fileList"
              :http-request="
                options => customCommonUpload(options, { type: 'fileList' })
              "
              :on-exceed="handleExceed"
              :on-remove="file => removeCommonFile(file, { type: 'fileList' })"
              :limit="1"
            >
              <template v-slot:trigger>
                <el-button type="primary">select file</el-button>
              </template>
            </el-upload>
        </el-form-item>
         <el-form-item label="config File" prop="fileConfigList">
            <el-upload
              v-model:file-list="formModel.fileConfigList"
              :http-request="
                options => customCommonUpload(options, { type: 'fileConfigList' })
              "
              :on-exceed="handleExceed"
              :on-remove="file => removeCommonFile(file, { type: 'fileConfigList' })"
              :limit="1"
            >
              <template v-slot:trigger>
                <el-button type="primary">select file</el-button>
              </template>
            </el-upload>
        </el-form-item>
</el-form>

script代码

javascript 复制代码
const uploadCancelTokenSource = ref<any>(null);
const uploadConfigCancelTokenSource = ref<any>(null);

//上传文件
const customCommonUpload = async (
  options: { file: File },
  params: { type: string }
) => {
  const { file } = options;
  const { type } = params;
  const formData = new FormData();
  formData.append("file", file); // 添加文件到表单数据中

  try {
    // 使用 axios 或其他 HTTP 库来发送请求
    uploadConfigCancelTokenSource.value = axios.CancelToken.source();
    uploadCancelTokenSource.value = axios.CancelToken.source();
    const response = await axios({
      method: "post",
      url: "your api", // 替换为你的上传地址
      data: formData,
      cancelToken:
        type == "fileList"
          ? uploadCancelTokenSource.value.token
          : uploadConfigCancelTokenSource.value.token
    });

    // 上传成功的处理逻辑
    if (type == "fileList") {
      formModel.fileId = response.data.result[0].fileId;
    } else if (type == "fileConfigList") {
      formModel.modelConfigFileId = response.data.result[0].fileId;
    }
    console.log(formModel, "formModel==========");

    return response;
  } catch (error) {
    // 上传失败的处理逻辑
    if (axios.isCancel(error)) {
      console.log("Request canceled", error.message);
    } else {
      console.error("Error uploading file:", error);
    }
    throw error;
  }
};
//取消上传
const removeCommonFile = (uploadFile: UploadFile, params: { type: string }) => {
  const { type } = params;
  if (type == "fileList") {
    // 如果有正在进行的上传请求,取消它
    if (uploadCancelTokenSource.value) {
      uploadCancelTokenSource.value.cancel("File upload canceled");
      uploadCancelTokenSource.value = null;
    }

    // 更新文件列表
    const index = formModel.fileList.indexOf(uploadFile);
    if (index !== -1) {
      formModel.fileId = "";
      formModel.fileList.splice(index, 1);
    }
  } else if (type == "fileConfigList") {
    // 如果有正在进行的上传请求,取消它
    if (uploadConfigCancelTokenSource.value) {
      uploadConfigCancelTokenSource.value.cancel("File upload canceled");
      uploadConfigCancelTokenSource.value = null;
    }

    // 更新文件列表
    const index = formModel.fileConfigList.indexOf(uploadFile);
    if (index !== -1) {
      formModel.modelConfigFileId = "";
      formModel.fileConfigList.splice(index, 1);
    }
  }
};
const handleExceed = (files, fileList) => {
  ElMessage.warning(`当前限制选择 1 个文件,请先删除已上传的文件`);
};
相关推荐
kyriewen23 分钟前
你的前端滤镜慢得像PPT?用Rust+WebAssembly,一秒处理4K图
前端·rust·webassembly
kyriewen1132 分钟前
你等的Babel编译,够喝三杯咖啡了——用Rust重写的SWC,只需眨个眼
开发语言·前端·javascript·后端·性能优化·rust·前端框架
IT_陈寒44 分钟前
SpringBoot自动配置坑了我,原来要这样绕过去
前端·人工智能·后端
东方小月1 小时前
Claude Code 完整上手指南:MCP、Skills、第三方模型配置一次搞定
前端·人工智能·后端
XZ探长2 小时前
基于 Trae Solo 移动办公修复 Vue3 前端服务问题
前端
逍遥德2 小时前
AI时代,计算机专业大学生学习指南
java·javascript·人工智能·学习·ai编程
蝎子莱莱爱打怪2 小时前
Claude Code 省 Token 小妙招:RTK + Caveman 组合拳
前端·人工智能·后端
Rkgua2 小时前
JS中模拟函数重载的使用
javascript·jquery
竹林8182 小时前
用 wagmi v2 和 Next.js 14 硬扛 NFT 市场前端:从合约调用失败到批量上架,我踩了这些坑
javascript·next.js
Momo__2 小时前
Vue 3.6 Vapor Mode:跳过虚拟 DOM,性能极致优化
前端·vue.js