This is a wrapper for react-native that uploads a file in the app documents directory to an external server. This a stripped down, dead simple version of react-native-file-upload by booxood (github.com/booxood/react-native-file-upload). If you want more control over the upload configuration (e.g. setting mimeType or the base directory of the upload file) please use react-native-file-upload. This is for ios.
You can use this package together with my other react-native packages. A typical workflow could be:
-
Check if file exists to upload - use react-native-check-file-exists-ios
-
If file does not exist, create blank file - use react-native-create-new-file-ios (then edit the file in your app)
-
Once file exists, you can upload using this package
npm install react-native-file-upload-to-server-ios --save
In XCode, in the project navigator, right click Libraries ➜ Add Files to [your project's name]
Go to node_modules ➜ react-native-file-upload-to-server-ios and add RNFileUploadToServer.xcodeproj
In XCode, in the project navigator, select your project. Add libRNFileUploadToServer.a to your project's Build Phases ➜ Link Binary With Libraries
Click RNFileUploadToServer.xcodeproj in the project navigator and go the Build Settings tab. Make sure 'All' is toggled on (instead of 'Basic'). Look for Header Search Paths and make sure it contains both
Run your project (Cmd+R)
Setup trouble?
If you get stuck, take a look at Brent Vatne's blog. His blog is my go to reference for this stuff.
var React = require('react-native');
var { NativeModules } = React;
var { RNFileUploadToServer } = NativeModules;
var uploadObject = {
filename: 'MyFile.txt',
uploadUrl: 'http:/www.example.com/upload.php',
// optional - key:value pairs for file metadata
fields: {
//'parameter1': 'value1',
//'parameter2': 'value2',
//'parameter3': 'value3',
},
};
RNFileUploadToServer.uploadFile(
uploadObject,
function errorCallback(results) {
console.log('Upload Error: ' + results['errMsg']);
},
function successCallback(results) {
console.log('Upload Success:' + results['successMsg']);
}
);
The following will cause an error callback (use the console.log to see the specific message):
-
File name not set in javascript
-
Upload url is not valid (less than 3 characters)
-
Server upload status other than a 200 response
Once the file is uploaded, you can process it on the server.
In php, you can use a var_dump($_FILES) to get details on the file. The parameters can be handled as basic post values. For example, in php, $_POST['parameter1']
In php, the name of the file becomes the
Special thanks to booxood and the work done on react-native-file-upload. Brent Vatne for his posts on creating a react native packager. Some portions of this code have been based on answers from stackoverflow. This package also owes a special thanks to the tutorial by Jay Garcia at Modus Create on how to create a custom react native module.