Expo & SpringBoot 分段上传问题

问题描述

我已经尝试了几个小时,使用分段文件上传来上传文件和 JSON。我使用 Expo React Native 作为客户端,SpringBoot 作为服务器。

我已经尝试了许多不同的版本。在阅读了很多内容之后,它应该是这样工作的:

在我的 Expo 应用中,我有这个:

const formData = new FormData();
formData.append(
    'document',new Blob([JSON.stringify(json)],{
    type: 'application/json'
}));
formData.append('file',{
    uri: url,type: data.type,name
});

const xhr = new XMLHttpRequest();
xhr.open('POST',API_URL);
xhr.setRequestHeader('Authorization','Bearer ' + jwt);
xhr.onload = () => {
    const response = JSON.parse(xhr.response);
    console.log(response);
    // ... do something with the successful response
};
xhr.onerror = e => {
    console.log(e,'upload failed');
};
xhr.ontimeout = e => {
    console.log(e,'upload timeout');
};
xhr.send(formData);

在我的 SpringBoot 后端,我有这个:

@PostMapping(value = "/api/upload")
public ResponseEntity<Void> uploadDocument(
    @RequestPart("document") DocumentDTO document,@RequestPart("file") MultipartFile file) {
    // ... my business logic
}

现在没有 document 它可以工作,但是一旦我添加 document 我得到这个错误: o.z.problem.spring.common.AdviceTraits : Bad Request: Required request part 'document' is not present

作为一种解决方法,我现在将文件作为 base64 编码的字符串上传......但我真的不明白为什么这不起作用,因为它应该。

我认为类似的问题: https://github.com/facebook/react-native/issues/30623

任何帮助将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)