/** React Native part */
var FileUpload = require('NativeModules').FileUpload;
_saveFile() {
var data = {
validity: this.state.value,
text: this.state.message,
location: this.props.location,
user: {
id: this.props.user._id,
username: this.props.user.username
}
};
var obj = {
uploadUrl: '/files',
method: 'POST', // default 'POST',support 'POST' and 'PUT'
headers: {
'Accept': 'application/json',
},
fields: data,
files: [
{
filename: 'test_file', // require, file name
filepath: file.uri, // require, file absoluete path
},
]
};
FileUpload.upload(obj, function (err, result) {
console.log('upload:', err, result);
});
}
/** node.js part */
var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
router.post('/files', multipartMiddleware, function(req, resp) {
console.log(req.body);
console.log(req.files);
// don't forget to delete all req.files when done
});
So far I believe this is the best way to upload files from RN to node.js server. Seems very stable.