3

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);
})
2
  • pass it as an array: github.com/joltup/… Commented Jul 25, 2019 at 6:32
  • @Horst I tried with a array, only comment is published without the file. How can a file be uploaded? Help me with this. Thanks. Commented Jul 25, 2019 at 7:16

2 Answers 2

1

Per documentation https://github.com/joltup/rn-fetch-blob#regular-request the Content-Type is automatically picked based on what you're trying to POST.

enter image description here

Here's an example on how to upload a file https://github.com/joltup/rn-fetch-blob#upload-a-file-from-storage

Sign up to request clarification or add additional context in comments.

Comments

-1

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');
  })

13 Comments

I tried this but can't upload the file. Both comment and file are empty uploaded without catching any error.
oh sorry i'm misstake, Content-Type should be json because your body type is json.
I modified my answer.
I changed 'Content-Type' to json but no any progess. Both comment as well as file is empty. Is only a file Uri is enough to upload ? How the 'var file' can be considered as actual file ? Help me with this. Thanks.
Sending a body in form data can be helpful. updated my answer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.