reactjs - rn-fetch-blob 错误:RNFetchBlob.fetchBlobForm 未能创建请求正文
问题描述
async postFileUpload(payload) {
const rnfetchfile = RNFetchBlob.wrap(payload.uri);
try {
console.log(
'POST',
'https://******.***.**/******/***/upload_*******_file',
{
...this.config,
'Content-Type': 'multipart/form-data',
},
[
// element with property `filename` will be transformed into `file` in form data
{
name: 'files',
filename: payload.name,
data: rnfetchfile.replace('file://file:///', 'file://'),
},
],
);
const res = await RNFetchBlob.fetch(
'POST',
'https://******.***.**/******/***/upload_*******_file',
{
...this.config,
'Content-Type': 'multipart/form-data',
},
[
// element with property `filename` will be transformed into `file` in form data
{
name: 'files',
filename: payload.name,
data: rnfetchfile.replace('file://file:///', 'file://'),
},
],
);
const response = JSON.parse(res.data);
console.log('api upload adpostimage', response);
return response;
} catch (err) {
console.log('postFileUpload', err.response, err);
Toast.show(err.response.data.message, Toast.SHORT);
throw err.response.data;
}
res 在控制台消息中抛出错误是
POST https://******.***.**/******/***/upload_*******_file {Content-Type: "multipart/form-data", Authorization: "Bearer ******.***.*****"}
[{…}]
0:
name: "files"
filename: "*****.pdf"
data: "RNFetchBlob-file://Users/********/tmp/*****/B****.pdf"}
postFileUpload undefined Error: RNFetchBlob.fetchBlobForm failed to create request body
at index.js:313
at MessageQueue.__invokeCallback (MessageQueue.js:483)
at MessageQueue.js:135
at MessageQueue.__guard (MessageQueue.js:384)
at MessageQueue.invokeCallbackAndReturnFlushedQueue (MessageQueue.js:134)
at debuggerWorker.js:69
我正在尝试使用 rn-fetch-blob 上传文件,并且由于 file://file/ 而发生了一些疯狂的事情 rnfetchfile.replace('file://file:///', 'file://') //输出看起来不正确我认为这主要是ios问题请帮帮我
解决方案
正如这里所说,只需使用decodeURIComponent(uri)
这是我的代码:
const realPath = Platform.OS === 'ios' ? uri.replace('file://', '') : uri;
const data = [
{
name: 'file',
filename,
type,
data: RNFetchBlob.wrap(decodeURIComponent(realPath)),
},
{name: 'bla', data: 'bla'}
]
推荐阅读
- powershell - 登录 GUI 脚本
- c# - 下载和解压 zip 文件错误:GZip 标头中的幻数不正确。确保您传入的是 GZip 流
- nameerror - NameError 显示在 rObject 上,而第 3 行中已经提到过
- javascript - 有没有办法在我的反应应用程序中看到上传的文档?
- c# - 通过 MassTransit 发布者/消费者传播 W3C 跟踪上下文
- reactjs - 如何让我的用户能够使用 React.js+Node App 添加自定义域
- django - 如何从 django 中基于类的详细视图中获取当前对象/产品?
- xml - 选择带有命名空间的 xml 文档的值
- ios - 如何在 iPhone OLED 上设置纯红色像素全面屏
- scala - 使用元组创建案例类的通用函数无法编译