|
| 1 | +# How to upload photo/file in react-native |
| 2 | + |
| 3 | +If you wanted to upload photo/file in react-native, almostly you have found many library such as [react-native-uploader](https://github.com/aroth/react-native-uploader) , [react-native-file-upload](https://github.com/booxood/react-native-file-upload). But I think easiest way is this post, not use library, not write native code, and anybody can understand. |
| 4 | + |
| 5 | +# use fetch |
| 6 | + |
| 7 | +Fetch supports `multipart/form-data`. You can upload your `formdata` like in webbrower. see [this](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Body) |
| 8 | + |
| 9 | + ### example |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +```jsx |
| 14 | +const data = new FormData(); |
| 15 | +data.append('name', 'testName'); // you can append anyone. |
| 16 | +data.append('photo', { |
| 17 | + uri: photo.uri, |
| 18 | + type: 'image/jpeg', // or photo.type |
| 19 | + name: 'testPhotoName' |
| 20 | +}); |
| 21 | +fetch(url, { |
| 22 | + method: 'post', |
| 23 | + body: data |
| 24 | +}).then(res => { |
| 25 | + console.log(res) |
| 26 | +}); |
| 27 | +``` |
| 28 | + |
| 29 | +only this. If you want many photos |
| 30 | + |
| 31 | +```js |
| 32 | +const photos = [photo1, photo2, ...] |
| 33 | +photos.forEach((photo) => { |
| 34 | + data.append('photo', { |
| 35 | + uri: photo.uri, |
| 36 | + type: 'image/jpeg', // or photo.type |
| 37 | + name: photos.name |
| 38 | + }); |
| 39 | +}); |
| 40 | +fetch(url, opts); |
| 41 | + |
| 42 | +``` |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +# How can I get photo from gallery? |
| 47 | + |
| 48 | +You can use [React Native API](https://facebook.github.io/react-native/docs/cameraroll.html), or other library such as [react-native-image-picker](https://github.com/marcshilling/react-native-image-picker), [react-native-camera-roll-picker](https://github.com/jeanpan/react-native-camera-roll-picker). |
| 49 | + |
| 50 | +#### Tips: |
| 51 | + |
| 52 | +In iOS+10, you must add |
| 53 | + |
| 54 | +```xml |
| 55 | + <key>NSPhotoLibraryUsageDescription</key> |
| 56 | + <string> Why you want Photo? </string> |
| 57 | +``` |
| 58 | + |
| 59 | +In Android, you must add |
| 60 | + |
| 61 | +```xml |
| 62 | + <uses-permission android:name="android.permission.CAMERA" /> |
| 63 | + <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> |
| 64 | + <uses-feature android:name="android.hardware.camera" android:required="false"/> |
| 65 | + <uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/> |
| 66 | + |
| 67 | +``` |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +# How can I add progress? |
| 72 | + |
| 73 | +Unfortunately, fetch doen't support progress. But we can use xhr which support progress. |
| 74 | + |
| 75 | +I make `api.js` |
| 76 | + |
| 77 | +```javascript |
| 78 | +const futch = (url, opts={}, onProgress) => { |
| 79 | + console.log(url, opts) |
| 80 | + return new Promise( (res, rej)=>{ |
| 81 | + var xhr = new XMLHttpRequest(); |
| 82 | + xhr.open(opts.method || 'get', url); |
| 83 | + for (var k in opts.headers||{}) |
| 84 | + xhr.setRequestHeader(k, opts.headers[k]); |
| 85 | + xhr.onload = e => res(e.target); |
| 86 | + xhr.onerror = rej; |
| 87 | + if (xhr.upload && onProgress) |
| 88 | + xhr.upload.onprogress = onProgress; // event.loaded / event.total * 100 ; //event.lengthComputable |
| 89 | + xhr.send(opts.body); |
| 90 | + }); |
| 91 | +} |
| 92 | +export default futch |
| 93 | + |
| 94 | +``` |
| 95 | + |
| 96 | +Use this. |
| 97 | + |
| 98 | +### example |
| 99 | + |
| 100 | +```javascript |
| 101 | +import futch from './api'; |
| 102 | + |
| 103 | +const data = new FormData(); |
| 104 | +data.append('name', 'testName'); |
| 105 | +data.append('photo', { |
| 106 | + uri: source.uri, |
| 107 | + type: 'image/jpeg', |
| 108 | + name: 'testPhotoName' |
| 109 | +}); |
| 110 | + |
| 111 | + |
| 112 | +futch(url, { |
| 113 | + method: 'post', |
| 114 | + body: data |
| 115 | +}, (progressEvent) => { |
| 116 | + const progress = progressEvent.loaded / progressEvent.total; |
| 117 | + console.log(progress); |
| 118 | +}).then((res) => console.log(res), (err) => console.log(err)) |
| 119 | +``` |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +# Sample |
| 124 | + |
| 125 | +use react-native-image-picker and react-native-camera-roll-picker(based on React Native CameraRoll API). |
| 126 | + |
| 127 | +## [Here](https://github.com/g6ling/react-native-fileUpload-example) |
| 128 | + |
0 commit comments