Quantcast
Channel: Active questions tagged react-native+android - Stack Overflow
Viewing all articles
Browse latest Browse all 29632

React Native fs RNFS.UploadFiles Failed to connect to localhost/127.0.0.1:7171

$
0
0

Here is the code that I'm using to upload a file from a react-native client to a nodejs server. However, when I call this function to upload the file to the server it throws this error Failed to connect to localhost/127.0.0.1:7171. I've tested the node server with postman and it works fine. Moreover, I tried the solution which sets android:usesCleartextTraffic="true" in the android manifest but this solution didn't work.

async uploadToNode() {
let testUrl = this.state.multipleFile[0].uri;
const split = testUrl.split('/');
const name = split.pop();
const setFileName = "file"
const inbox = split.pop();
const realPath = `${RNFS.TemporaryDirectoryPath}${inbox}/${name}`;
console.log(realPath);
const uploadUrl = "http://localhost:7171/uploadToIpfs";

var file = [{
  name: "file",
  filename:name,
  filepath: realPath,
}];

var uploadBegin = (response) => {
  const jobId = response.jobId;
  console.log('UPLOAD HAS BEGUN! JobId: ' + jobId);
};

var uploadProgress = (response) => {
  const percentage = Math.floor((response.totalBytesSent / response.totalBytesExpectedToSend) * 100);
  console.log('UPLOAD IS ' + percentage + '% DONE!');
};

 RNFS.uploadFiles({
  toUrl: uploadUrl,
  files: file,
  method: 'POST',
  headers: {
     'Accept': 'application/json',
  },
  begin: uploadBegin,
  progress: uploadProgress
  })
  .promise.then((response) => {
    console.log(response,"<<< Response");
    if (response.statusCode == 200) { //You might not be getting a statusCode at all. Check
       console.log('FILES UPLOADED!');
     } else {
       console.log('SERVER ERROR');
      }
    })
    .catch((err) => {
      if (err.description) {
        switch (err.description) {
          case "cancelled":
            console.log("Upload cancelled");
            break;
          case "empty":
            console.log("Empty file");
          default:
           //Unknown
        }
      } else {
       //Weird
      }
      console.log("Some Error"+ err);
   });
}

Viewing all articles
Browse latest Browse all 29632

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>