So far I believe this is the best way to upload files from RN to node.js server. Seems very stable.
Last active
October 26, 2017 04:31
-
-
Save piggydoughnut/0c379ade6cd3b1626a73c445495a351a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 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 | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
npm install connect-multiparty