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

上传多文件失败 #36

Open
Pomelo2021 opened this issue Sep 25, 2024 · 5 comments
Open

上传多文件失败 #36

Pomelo2021 opened this issue Sep 25, 2024 · 5 comments

Comments

@Pomelo2021
Copy link

  // 提示参数 files?: Blob[];
 Apis.device.apiDeviceFilesPost({data: {files: [fileList.value[0].raw, fileList.value[1].raw]}}); //失败
 Apis.device.apiDeviceFilesPost({data: formData}); //正常

如下,生成api代码修改后成功,仅供参考,我js很菜

if (Object.prototype.toString.call(data) === '[object Object]' && typeof FormData !== 'undefined') {
                let hasBlobData = false;
                const formData = new FormData();
                for (const key in data) {
                    if (Object.prototype.toString.call(data[key]) === "[object Array]") {
                        for (const ele of data[key]) {
                            formData.append(key, ele);
                            if (ele instanceof Blob) {
                                hasBlobData = true;
                            }
                        }
                    } else {
                        formData.append(key, data[key]);
                        if (data[key] instanceof Blob) {
                            hasBlobData = true;
                        }
                    }
                }
                data = hasBlobData ? formData : data;
            }
@JOU-amjs
Copy link
Contributor

默认情况下,提交数据对象内包含Blob将会自动转换为FormData,看你的情况,文件被放在了嵌套对象内,什么情况下要这样上传文件呢?

@Pomelo2021
Copy link
Author

Pomelo2021 commented Oct 26, 2024

默认情况下,提交数据对象内包含Blob将会自动转换为FormData,看你的情况,文件被放在了嵌套对象内,什么情况下要这样上传文件呢?

apiDeviceFilesPost<
        Config extends Alova2MethodConfig<RESTfulResult_String> & {
          data: {
            files?: Blob[];
          };
        }
      >(
        config: Config
      ): Alova2Method<RESTfulResult_String, 'device.apiDeviceFilesPost', Config>;

上传单文件,会自动包装成formdata。
我那样操作是按照方法数据类型提示来的,上传多文件时,就不会自动包装成formdata了,会失败。

@JOU-amjs
Copy link
Contributor

@Pomelo2021 你是指你的接口传的是一个文件数组,然后导致失败的对吗

@Pomelo2021
Copy link
Author

@Pomelo2021 你是指你的接口传的是一个文件数组,然后导致失败的对吗

对啊,传的数组,就不会自动包装成formdata了

 if (Object.prototype.toString.call(data) === '[object Object]' && typeof FormData !== 'undefined') {
        let hasBlobData = false;
        const formData = new FormData();
        for (const key in data) {
          formData.append(key, data[key]);
          if (data[key] instanceof Blob) {
            hasBlobData = true;
          }
        }
        data = hasBlobData ? formData : data;
      }

@JOU-amjs
Copy link
Contributor

好的,我们优化下

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