How to upload file to server using react-native

后端 未结 7 1318
渐次进展
渐次进展 2020-12-12 20:36

I am developing a app where i need to upload an image to the server. Based on the image i get a response which i need to render?.

Can you please help me how to uploa

7条回答
  •  醉梦人生
    2020-12-12 21:17

    Tom's answer didn't work for me. So I implemented a native FilePickerModule which helps me choose the file and then use the remobile's react-native-file-transfer package to upload it. FilePickerModule returns the path of the selected file (FileURL) which is used by react-native-file-transfer to upload it. Here's the code:

    var FileTransfer = require('@remobile/react-native-file-transfer');
    var FilePickerModule = NativeModules.FilePickerModule;
    
          var that = this;
          var fileTransfer = new FileTransfer();
          FilePickerModule.chooseFile()
          .then(function(fileURL){
            var options = {};
            options.fileKey = 'file';
            options.fileName = fileURL.substr(fileURL.lastIndexOf('/')+1);
            options.mimeType = 'text/plain';
            var headers = {
              'X-XSRF-TOKEN':that.state.token
            };
            options.headers = headers;
            var url = "Set the URL here" ;
            fileTransfer.upload(fileURL, encodeURI(url),(result)=>
            {
                  console.log(result);
              }, (error)=>{
                  console.log(error);
              }, options);
         })
    

提交回复
热议问题