dio_file_uploader 0.1.1 copy "dio_file_uploader: ^0.1.1" to clipboard
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 #

  1. Create a new instance of DioFileHandler, DioChunkedFileHandler or DioRestorableChunkedFileHandler.
  2. Create a FileUploadController with the created handler
  3. 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!");
}
3
likes
150
pub points
20%
popularity

Publisher

verified publishermattiapispisa.it

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.

Homepage
Repository (GitHub)
View/report issues

Topics

#dio #file #upload #chunks #retry

Documentation

API reference

License

MIT (license)

Dependencies

dio, en_file_uploader, file_uploader_socket_interfaces

More

Packages that depend on dio_file_uploader