dio_file_uploader 0.1.1 dio_file_uploader: ^0.1.1 copied to clipboard
A en_file_uploader plugin to handle the file upload using dio package. Provides the capability to upload a file in chunks with built-in retry handling.
Dio File Uploader #
A en_file_uploader plugin to handle the file upload using dio package.
dio_file_uploader
requires en_file_uploader
package.
More info about en_file_uploader
here
Features #
- use
DioFileHandler
to handle a file upload; - use
DioChunkedFileHandler
to handle a file upload in chunk; - use
DioRestorableChunkedFileHandler
to handle a file upload in chunk with the capability to restore the upload.
Usage #
- Create a new instance of
DioFileHandler
,DioChunkedFileHandler
orDioRestorableChunkedFileHandler
. - Create a
FileUploadController
with the created handler - Call
controller.upload
import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:dio_file_uploader/dio_file_uploader.dart';
import 'package:en_file_uploader/en_file_uploader.dart';
import 'package:file_uploader_utils/file_uploader_utils.dart' as utils;
main() async {
final client = Dio();
final file = utils.createIoFile();
final baseRequestPath = "my-request";
final headers = {"Authorization": "Bearer XXX"};
final restorableHandler = DioRestorableChunkedFileHandler(
client: client,
file: XFile(file.path),
presentMethod: "POST",
chunkMethod: "PATCH",
statusMethod: "HEAD",
presentPath: "$baseRequestPath",
chunkPath: (presentation, _) => "$baseRequestPath&patch=${presentation.id}",
statusPath: (presentation) => "$baseRequestPath&status=${presentation.id}",
presentHeaders: {
"Upload-Length": file.lengthSync().toString(),
...headers,
},
chunkHeaders: (presentation, chunk) {
return headers;
},
statusHeaders: null,
presentParser: (response) =>
FileUploadPresentationResponse(id: response.data),
statusParser: (response) =>
FileUploadStatusResponse(nextChunkOffset: jsonDecode(response.data)),
chunkSize: 1024 * 1024, // 1mb
presentBody: null,
chunkBody: null,
statusBody: null,
);
final controller = FileUploadController(
restorableHandler,
logger: utils.PrintLogger(),
);
await controller.upload();
print("done!");
}