Avue2图片上传使用object对象模式时,axios的请求模式用post还是get?

Avue2图片上传使用object对象模式时,axios的请求模式用post还是get?

Upload对象如下:

javascript 复制代码
/**
 * 上传附件
 * @param {*} vueObj
 * @param {*} row
 * @returns
 */
export const uploadOnlyOption = (vueObj, row) => {
  return {
    tip: true,
    // border: true, //显示边框
    labelWidth: 120, //操作菜单栏的宽度
    span: 12,
    submitBtn: false,
    emptyBtn: false,
    menuBtn: false,
    detail: false,
    column: [
      {
        row: true,
        span: 24,
        label: "结账附件",
        prop: "checkout_attachment",
        type: "upload",
        listType: "picture-card",
        // listType: "text",
        // dragFile: true, //拖拽上传
        accept:
          "image/png, image/jpeg, image/gif, application/pdf,application/x-zip-compressed,.rar,application/x-rar-compressed,application/x-7z-compressed,application/x-bzip,application/x-bzip2",
        limit: 10,
        fileSize: 10000,
        dataType: "object",
        span: 24,
        fileText: "选择附件",
        loadText: "上传附件中...",
        props: {
          label: "name",
          value: "url",
        },
        propsHttp: {
          res: "data",
          // home: "domain",
          name: "name",
          url: "url",
        },
        canvasOption: {
          text: "  ",
          ratio: 0.3,
        },
        tip: "上传"jpg/png/pdf/压缩包"格式的文件,最多10个,且不超过10M,重新上传时请记住点击提交按钮保存!",
        action: vueObj.attachUploadUrl,
        uploadPreview: (file, column, done) => {
          let previewType = ["jpg", "jpeg", "gif", "bmp", "png"];
          let suffix = file.url.substring(file.url.lastIndexOf(".") + 1);
          if (previewType.includes(suffix.toLowerCase())) {
            done(); //默认执行预览
          } else {
            vueObj
              .$confirm("该文件无法预览,可以下载后查看。", "提示", {
                confirmButtonText: "下载",
                cancelButtonText: "取消",
              })
              .then(() => {
                vueObj.$downloadFile(file.url, file.name);
              })
              .catch(() => { });
          }
        },
      },
    ],
  };
};

dataType用object对象,支持name,url,label,value等多对象属性上传,方便后端数据存储。

上传操作方法如下:

javascript 复制代码
uploadRecordDetailError(error, column) {
  this.$message.error(error + "!");
 },
 uploadRecordDetailExceed(limit, files, fileList, column) {
     if (fileList.length >= limit) {
         this.$message.error(`上传文件数量不能超过${limit}个!`);
     }
 },
 uploadRecordDetailBefore(file, done, loading, column) {
     // console.log(file, column);
     let suffix = file.name.substring(file.name.lastIndexOf(".") + 1);
     if (column.accept.indexOf(file.type) >= 0 || suffix.toLowerCase() == "rar") {
         done();
     } else {
         this.$message.error(`请先上传${column.accept}文件!`);
         loading();
     }
 },
 uploadRecordDetailAfter(res, done, loading, column, ) {
     // console.log(res, this.model);
     let uploadFile = {
         attachPath: res.url,
         attachName: res.name,
     };
     done();
     // this.$message.success("上传后的方法");
 },
 uploadRecordDetailDelete(file, column) {
     // console.log(file, column);
     return new Promise((resolved, reject) => {
         resolved("success");
     });
 },
 //下载附件
 downAttachFile(file) {
     this.$downloadFile(file.url, file.name);
 },

提交上传方法:

javascript 复制代码
 this.$confirm("是否上传!", "提示", {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
}).then((data) => {
    axios
        .post(this.checkoutUrl, {
            _csrf,
            id: this.mainData.id,
            checkout_attachment: this.mainData.checkout_attachment,
        }, )
        .then((res) => {
            if (res.status == 200 && res.data.code === 0) {
                this.$emit("reload");
                this.$message({
                    type: "success",
                    message: "上传成功!",
                });
                this.close();
            } else {
                this.$message({
                    type: "error",
                    message: "执行操作出错!",
                });
            }
        });
}).catch(() => {});

请注意,上述代码使用到的是axios.post,这样checkout_attachment以表单对象数据提交给后端,方面后续数据的存储使用。

不推荐的方式

使用axios.get方式的不便之处,当上传多个文件时,get方法会将checkout_attachment以string数组(array)方式上传,后端接收后需要额外转码才能存储在结构数据中,相比使用post麻烦。

相关推荐
QQ1__8115175151 分钟前
Spring boot名城小区物业管理系统信息管理系统源码-SpringBoot后端+Vue前端+MySQL【可直接运行】
前端·vue.js·spring boot
钛态3 分钟前
前端微前端架构:大项目的救命稻草还是自找麻烦?
前端·vue·react·web
一粒黑子4 分钟前
【实战解析】阿里开源 PageAgent:纯前端 GUI Agent,一行JS让网页支持自然语言操控
前端·javascript·开源
独角鲸网络安全实验室5 分钟前
2026微信小程序抓包全解析:从实操落地到合规风控,解锁前端调试新范式
前端·微信小程序·小程序·抓包·系统代理绕过·https证书严格校验·进程隔离
紫微AI6 分钟前
前端文本测量成了卡死一切创新的最后瓶颈,pretext实现突破了
前端·人工智能·typescript
GISer_Jing6 分钟前
AI前端(From豆包)
前端·aigc·ai编程
IT枫斗者6 分钟前
前端部署后如何判断“页面是不是最新”?一套可落地的版本检测方案(适配 Vite/Vue/React/任意 SPA)
前端·javascript·vue.js·react.js·架构·bug
测试修炼手册6 分钟前
[测试技术] 深入理解 JSON Web Token (JWT)
前端·json
AI老李9 分钟前
2026 年 Web 前端开发的 8 个趋势!
前端
里欧跑得慢10 分钟前
15. Web可访问性最佳实践:让每个用户都能平等访问
前端·css·flutter·web