Rahul Shakya
Rahul Shakya

Reputation: 1425

rn-fetch-blob Error: RNFetchBlob.fetchBlobForm failed to create request body

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 throws error in console messages are

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

I am trying to upload a file using rn-fetch-blob and some crazy things happened rnfetchfile.replace('file://file:///', 'file://'), because of file://file/// outputting which didn't seem right I think this like mostly ios problem please help me out guys

Upvotes: 6

Views: 10232

Answers (2)

Reza Babaei
Reza Babaei

Reputation: 1075

as here said just use decodeURIComponent(uri)

this is my code :

const realPath = Platform.OS === 'ios' ? uri.replace('file://', '') : uri;

const data = [
  {
     name: 'file',
     filename,
     type,
     data: RNFetchBlob.wrap(decodeURIComponent(realPath)),
  },
  {name: 'bla', data: 'bla'}
]

Upvotes: 22

Rahul Shakya
Rahul Shakya

Reputation: 1425

Try replacing the file://file:// to '' an empty string..

rnfetchfile.replace('file://file:///', '') 

– @Abhishek Ghosh commented is a need trick which will work but the file will not be available in the uri.. your uploaded file wont open

I tried doing

data: decodeURIComponent(rnfetchfile.replace('file://file:///', 'file:///')

and it worked hope this helps

Upvotes: 0

Related Questions