Skip to content

Instantly share code, notes, and snippets.

@piggydoughnut
Last active October 26, 2017 04:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piggydoughnut/0c379ade6cd3b1626a73c445495a351a to your computer and use it in GitHub Desktop.
Save piggydoughnut/0c379ade6cd3b1626a73c445495a351a to your computer and use it in GitHub Desktop.

So far I believe this is the best way to upload files from RN to node.js server. Seems very stable.

/** 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
});
@piggydoughnut
Copy link
Author

npm install connect-multiparty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment