reactjs - rn-fetch-blob 错误 : RNFetchBlob. fetchBlobForm 未能创建请求正文

标签 reactjs react-native react-redux react-native-ios rn-fetch-blob

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 上传文件,但发生了一些疯狂的事情
rnfetchfile.replace('file://file:///', 'file://'), 因为 file://file///输出似乎不对
我认为这主要是 ios 问题,请大家帮帮我

最佳答案

here说只是用 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'}
]

关于reactjs - rn-fetch-blob 错误 : RNFetchBlob. fetchBlobForm 未能创建请求正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60186820/

相关文章:

javascript - React - 元素类型无效 : expected a string

reactjs - 配置 redux 在所有浏览器上显示无法正常工作

javascript - React 点击处理函数在 2 次点击后开始工作

javascript - react .js : onClick function from child to parent

html - 父 div 标签改变样式

android - 任务 ':app:mergeDebugResources' 执行失败 - AwesomeProject

javascript - react-native-router-flux 警告 : Key is already defined

javascript - 是否可以动态地将 JSX 添加到 React 代码中

image - 当图像(来自react-native)抛出 onError 时渲染文本组件

javascript - 收到错误 : `ReferenceError: Cannot access ' imported const' before initialization` in react