PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, July 31, 2022

[FIXED] How can I upload a file in react-native using rn-fetch-blob?

 July 31, 2022     fetch, file-upload, react-native     No comments   

Issue

I am new to react-native. I wanted to upload a file with another parameter 'comment' using rn-fetch-blob. I am able to pick a file and got path, name, type and size of file using react-native-document-picker package. The log for the details of file is:

console.log(res.uri, res.type, res.name, res.size);
Output:
'content://com.android.providers.downloads.documents/document/1445', 'text/html', 'hello.webp', 21476

I simply used fetch function with method 'POST' , not sure on this.

var file = {
        name: this.type,
        filename : this.name, 
        data: RNFetchBlob.wrap(this.uri)
    };

Log of var file:

{ name: 'text/html',
│ filename: 'hello.webp',
└ data: 'RNFetchBlob-content://content://com.android.providers.downloads.documents/document/1445' }

method:

fetch('https://beta.hellonepal.io/api/v1/comments',
          {
            method: 'POST',
            headers:
            {
              'Accept': 'application/json',
              'Content-Type' : 'multipart/form-data',
              'Authorization': 'Bearer '+ global.apiToken,
            },
            body: JSON.stringify(
            {
              comment: this.state.newComment,
              comment_file : file
            })

          })
          .then(response => {
            return response.json()
          })
          .then(RetrivedData => {
            console.log(RetrivedData);

          })
          .catch(err => {
            // Do something for an error here
            console.log('Error in adding a comment');
          });
        });

I tried using RNFetchBlob method but no idea how can I pass others parameters:

const url = "https://beta.hellonepal.io/api/v1/comments";

RNFetchBlob
.config({
    trusty : true
})
.fetch(
    'POST', 
    url, {
        'Content-Type' : 'multipart/form-data',
    }, file)
.then((res) => {
   console.log(res);
   callback(res);
})
.catch((errorMessage, statusCode) => {
    console.log("Error: "+ errorMessage + " - " + statusCode);
})

Solution

You can change the upload as follows:

Content-Type should be json because your body type is json

 let formdata = new FormData();
     formdata.append("comment", this.state.newComment);
     formdata.append("comment_file", file);
RNFetchBlob.fetch('POST', 'https://beta.hellonepal.io/api/v1/comments', {
    Authorization : 'Bearer '+ global.apiToken,
    body: formdata,
    'Content-Type' : "multipart/form-data",
  })
  .then((response) => response.json())
  .then((RetrivedData) => {
      console.log(RetrivedData);
  })
  .catch((err) => {
    console.log('Error in adding a comment');
  })


Answered By - hong developer
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing