Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

多文件上传接口后台api应该返回什么格式的json数据? #869

Open
foxfire881 opened this issue Aug 13, 2024 · 2 comments
Open

Comments

@foxfire881
Copy link

foxfire881 commented Aug 13, 2024

看了下 文档中的example 没看懂。

1、请问多文件上传,只是在文件选择框中允许多选文件了,实际上传时还是遍历文件数组,调用单文件上传接口一个一个去上传的?还是文件数组作为一个参数、在一个request中上传所有文件?在example的代码没看到哪里是发送request的上传请求呢,从example中的for循环 for (const file of files) {...} 看,似乎还是一个一个上传的

2、多文件上传会有传成功的,也会有失败的,example中没看到失败处理的相关代码?

3、后台api应该返回什么样的json格式呢?

@sunsonliu
Copy link
Collaborator

额,这个接口跟后台返回格式无关,主要是调用callback时传入一个数组就好了,数组格式如下:

[
  {
    url: string,
    params: {
      name: string,
      isShadow: boolean,
      width: string,
      height: string,
      ....
    },
  },
  {
    url: string,
  },
]

至于上传失败如何处理,则需要业务方自行实现相关异常的处理机制哈

@foxfire881
Copy link
Author

foxfire881 commented Aug 13, 2024

搞定了~ 贴出来给后来的参考下:

function fileUploadMulti(files, callback) {
  const formData = new FormData();
  for (let i = 0; i < files.length; i++) {
    formData.append("files", files[i]);
  }

  axios
    .post("/upload", formData, {
      headers: { "Content-Type": "multipart/form-data" },
    })
    .then((response) => {
      const { data } = response;
      let succ = data.succList;
      let err = data.errList;
      const promises = [];
      for (let i = 0; i < succ.length; i++) {
        const promise = new Promise((resolve) => {
         ...
        });
        promises.push(promise);
      }

      for (let i = 0; i < err.length; i++) {
        const promise = new Promise((resolve) => {
           ...
        })
        promises.push(promise);
      }
      Promise.all(promises).then((results) => {
        callback(results);
      });
    })
    .catch((error) => {
      console.log("err");
      console.error(error);
    });

后台返回数据格式:

{
    "msg": "",
    "data": {
        "errList": [],
        "succList": [
            {
                "name": "test1.png",
                "path": "/file/99262f59.png",
                "type": "image/png"
            },
            {
                "name": "test2.png",
                "path": "/file/134f201a.png",
                "type": "image/png"
            }
        ]
    },
    "status": 0
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants