如何在服务器端获取react-native-fs上传的文件

时间:2018-12-11 06:28:43

标签: reactjs react-native audio file-upload react-native-fs

我正在使用react-native-audio进行录制。要将音频文件上传到服务器端,我先使用了formdata&fetch,但由于出现错误

  

网络请求失败

所以我用了react-native-fs,在这里上传成功了,但是我不知道如何在服务器端访问上传的文件以将其保存到服务器的文件夹中

var url = this.props.url+'app/complaint/uploadAudiofile?id=1';
    RNFS.uploadFiles({
      toUrl: url,
      files: files,
      method: 'POST',
      headers: {
        'Accept': 'application/json',
      },
      fields: {
        'hello': 'world',
      },
      begin: uploadBegin,
      progress: uploadProgress
    })
    .promise.then((response) => {
      if (response.statusCode == 200) {
        console.log('FILES UPLOADED!');
      } 
      else {
        console.log('SERVER ERROR');
      }
    })
    .catch((err) => {
      if(err.description === "cancelled") {
      // cancelled by user
      }
      console.log(err);
    });

我无法访问服务器端的文件

uploadAudiofile: function(req,callback){
  var util = require('util');
  var path = require('path');
  var folder = '../static/images/complaint/' + date;
  var files = req.param('files');

  sails.log.debug("PATH : ", path.resolve(sails.config.appPath, folder));
  console.log("iddd  :"+id);
  d.run(function safelyUpload () {
  var file=req.file('file').upload({dirname: path.resolve(sails.config.appPath, folder),
    }, function whenDone(err, uploadedFiles) {
      if (err) return res.serverError(err);
      else{
        sails.log.debug('Complaint File data : ' +util.inspect(uploadedFiles, {showHidden: true,depth: null}));
        console.log("uploaded file  :"+uploadedFiles[0].fd);
        callback({files: uploadedFiles,textParams: req.allParams()});
      }
    });
  });
  },

0 个答案:

没有答案