Flutter UpChunk
Flutter UpChunk is a simple port of the JS library https://github.com/muxinc/upchunk done by MUX, Inc.
Installation
Add the package to the dependencies section in pubspec.yaml:
flutter_upchunk: ^2.1.0(or latest release)
Usage
Add the following import to the .dart file that will use UpChunk
import 'package:flutter_upchunk/flutter_upchunk.dart';
Example
final uploadUrl = '';
final filePath = '';
// Chunk upload
var upChunkUpload = UpChunk(
endPoint: uploadUrl,
file: XFile(filePath),
onProgress: (progress) {
print('Upload progress: ${progress.ceil()}%');
},
onError: (String message, int chunk, int attempts) {
print('UpChunk error 💥 🙀:');
print(' - Message: $message');
print(' - Chunk: $chunk');
print(' - Attempts: $attempts');
},
onSuccess: (response) {
print('Upload complete! 👋 (status: ${response.statusCode})');
},
);
Or await the upload instead of using callbacks:
final upChunk = UpChunk(
endPoint: uploadUrl,
file: XFile(filePath),
onProgress: (progress) => print('Upload progress: ${progress.ceil()}%'),
);
final response = await upChunk.done; // throws on failure or stop()
print('Upload complete! 👋 (status: ${response.statusCode})');
API
Although the API is a port of the original JS library, some options and properties differ slightly.
UpChunk constructor:
Upload options
-
endPointtype:string(required)URL to upload the file to.
-
filetype:XFile(required)The file you'd like to upload.
-
headerstype:Map<String, String>A
Mapwith any headers you'd like included with thePUTrequest for each chunk. -
chunkSizetype:integer, default:5120The size in kb of the chunks to split the file into, with the exception of the final chunk which may be smaller. This parameter should be in multiples of 64.
-
attemptstype:integer, default:5The number of times to retry any given chunk.
-
delayBeforeRetrytype:integer, default:1The time in seconds to wait before attempting to upload a chunk again.
-
chunkStarttype:integer, default:nullThe chunk number to start the upload from, useful in case the uploads fails in
xchunk and the instance to the object is lost, UpChunk can pick up the upload from there.
Event options
-
onAttempt{ chunkNumber: Integer, chunkSize: Integer }Fired immediately before a chunk upload is attempted.
chunkNumberis the number of the current chunk being attempted, andchunkSizeis the size (in bytes) of that chunk. -
onAttemptFailure{ message: String, chunkNumber: Integer, attemptsLeft: Integer }Fired when an attempt to upload a chunk fails.
-
onError{ message: String, chunk: Integer, attempts: Integer }Fired when a chunk has reached the max number of retries or the response code is fatal and implies that retries should not be attempted.
-
onProgressprogress double [0..100]Fired continuously with incremental upload progress. This returns the current percentage of the file that's been uploaded.
-
onSuccess{ response: Response }Fired when the upload is finished successfully, with the HTTP response of the final chunk.
UpChunk Instance Methods
-
pause()Pauses an upload after the current in-flight chunk is finished uploading.
-
resume()Resumes an upload that was previously paused.
-
restart()Restarts the upload from chunk
0, use only if and afteronErrorwas fired. -
stop()Cancels the upload abruptly.
restart()can be used to start the upload from chunk0.
UpChunk Instance Properties
-
doneA
Future<Response>that completes with the final chunk's HTTP response when the upload finishes, or completes with an error when the upload fails orstop()is called. Afterrestart()it reflects the new attempt.
Note: connection drops are handled by the retry logic (
attemptsretries,delayBeforeRetryseconds apart) and end inonError— there is no internet-connection watchdog anymore.
Credit
Original code by MUX, Inc. and ported to Dart 🎯 with ❤ by a Flutter developer.