chunkedUploadFile method
Future<bool>
chunkedUploadFile(
- Uri url,
- String serverType,
- String uploadPath,
- String uploadName,
- String fileType,
- Uint8List fileBytes, {
- void progressCallback()?,
override
Implementation
@override
Future<bool> chunkedUploadFile(
Uri url,
String serverType,
String uploadPath,
String uploadName,
String fileType,
Uint8List fileBytes, {
void Function(int loaded, int total)? progressCallback,
}) async {
try {
final int fileSize = fileBytes.length;
const int chunkSize = 6 * 1024 * 1024;
final int totalChunks = (fileSize / chunkSize).ceil();
int startBytes = 0;
for (int i = 1; i <= totalChunks; i++) {
final endBytes = min(startBytes + chunkSize, fileSize);
final chunk = fileBytes.sublist(startBytes, endBytes);
final multipartFile = http.MultipartFile(
'file',
Stream.fromIterable([chunk]),
fileSize,
filename: uploadName,
contentType: MediaType.parse(fileType),
);
final request = http.MultipartRequest('POST', url)
..fields['serverType'] = serverType
..fields['uploadPath'] = uploadPath
..fields['totalChunks'] = '$totalChunks'
..fields['currentChunk'] = '$i'
..files.add(multipartFile);
final resp = await request.send();
await resp.stream.bytesToString();
startBytes = endBytes;
if (progressCallback != null) {
progressCallback(startBytes, fileSize);
}
if (resp.statusCode != 200) {
return false;
}
}
return true;
} catch (err) {
logger.severe('Error at StorageUtilImpl.chunkedUploadFile >>> $err');
return false;
}
}