A package to bring Chunked File Upload / Resumable File Upload into React Native. Split a large file into multiple smaller pieces then upload them without worrying about network disconnection, even if it happens React Native Chunk Upload will only upload
npm install react-native-chunk-upload.digIn(file instead of files, next, retry, unlink);
react-native-fs https://github.com/itinance/react-native-fs
rn-fetch-blob https://github.com/joltup/rn-fetch-blob
bash
npm i react-native-chunk-upload
`
* via Yarn
`bash
yarn add react-native-chunk-upload
`
Basic Usage
`javascript
import Axios from 'axios';
import ChunkUpload from 'react-native-chunk-upload';
const chunk = new ChunkUpload({
path: response.path, // Path to the file
size: 10095, // Chunk size (must be multiples of 3)
fileName: response.fileName, // Original file name
fileSize: response.size, // Original file size
// Errors
onFetchBlobError: (e) => console.log(e),
onWriteFileError: (e) => console.log(e),
});
chunk.digIn(this.upload.bind(this));
upload(file, next, retry, unlink) {
const body = new FormData();
body.append('video', file.blob); // param name
Axios.post('ā URL HERE ā', body, {
headers: {
"Content-Type": "multipart/form-data",
"Accept": 'application/json',
// š„ Choose one of the following methods:
// 1ļøā£ If you're using the wester-chunk-upload php library...
...file.headers,
// 2ļøā£ Customize the headers
"x-chunk-number": file.headers["x-chunk-number"],
"x-chunk-total-number": file.headers["x-chunk-total-number"],
"x-chunk-size": file.headers["x-chunk-size"],
"x-file-name": file.headers["x-file-name"],
"x-file-size": file.headers["x-file-size"],
"x-file-identity": file.headers["x-file-identity"]
}
})
.then(response => {
switch (response.status) {
// ā
done
case 200:
console.log(response.data);
break;
// š still uploading...
case 201:
console.log(${response.data.progress}% uploaded...);
next();
break;
}
})
.catch(error => {
// ā waddafuk? š
if (error.response) {
if ([400, 404, 415, 500, 501].includes(error.response.status)) {
console.log(error.response.status, 'Failed to upload the chunk.');
unlink(file.path);
} else if (error.response.status === 422) {
console.log('Validation Error', error.response.data);
unlink(file.path);
} else {
console.log('Re-uploading the chunk...');
retry();
}
} else {
console.log('Re-uploading the chunk...');
retry();
}
});
}
`
$3
If you're going to use this library, you won't need much to do...
`javascript
// easy peasy, right? š
headers: {
"Content-Type": "multipart/form-data",
"Accept": 'application/json',
...file.headers
}
`
* https://github.com/hossein-zare/wester-chunk-upload
Schema
`javascript
chunk.digIn(
(
file: {
path: string,
headers: {
"x-chunk-number": number,
"x-chunk-total-number": number,
"x-chunk-size": number,
"x-file-name": string,
"x-file-size": number,
"x-file-identity": string
},
blob: {
name: string,
type: string,
uri: string
}
},
next: () => void,
retry: () => void,
unlink: (path: string) => void
): void
): void;
``