Giter VIP home page Giter VIP logo

flutter-upchunk-updated's Introduction

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: ^1.5.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

Future<String> _getUploadUrl() {
  String uploadUrl;
  // Perform call either to your API or directly to MUX to retrieve the upload URL
  // ...
  //

  return uploadUrl;
}

// Chunk upload
var uploadOptions = UpChunkOptions()
  ..endPointResolver = _getUploadUrl()
  ..file = File(_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 = () {
    print('Upload complete! ๐Ÿ‘‹');
  };
var upChunkUpload = UpChunk.createUpload(uploadOptions);

API

Although the API is a port of the original JS library, some options and properties differ slightly.

createUpload(UpChunkOptions options)

Returns an instance of UpChunk and begins uploading the specified File.

UpChunkOptions parameters:

Upload options
  • endPoint type: string (required if endPointResolver is null)

    URL to upload the file to.

  • endPointResolver type: Future<String> (required if endPoint is null)

    A Future that returns the URL as a String.

  • file type: File (required)

    The file you'd like to upload.

  • headers type: Map<String, String>

    A Map with any headers you'd like included with the PUT request for each chunk.

  • chunkSize type: integer, default:5120

    The 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.

  • attempts type: integer, default: 5

    The number of times to retry any given chunk.

  • delayBeforeRetry type: integer, default: 1

    The time in seconds to wait before attempting to upload a chunk again.

Event options
  • onAttempt { chunkNumber: Integer, chunkSize: Integer }

    Fired immediately before a chunk upload is attempted. chunkNumber is the number of the current chunk being attempted, and chunkSize is 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.

  • onOffline

    Fired when the client has gone offline.

  • onOnline

    Fired when the client has gone online.

  • onProgress progress double [0..100]

    Fired continuously with incremental upload progress. This returns the current percentage of the file that's been uploaded.

  • onSuccess

    Fired when the upload is finished successfully.

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 after onError was fired.

  • stop()

    Cancels the upload abruptly. restart() can be used to start the upload from chunk 0.

Credit

Original code by MUX, Inc. and ported to Dart ๐ŸŽฏ with โค by a Flutter developer.

flutter-upchunk-updated's People

Contributors

gabrielginter avatar dalarin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.