Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

flutter uploader

a plugin for creating and managing upload tasks. supports ios and android.

this plugin is based on workmanager in android and nsurlsessionuploadtask in ios to run upload task in background mode.

this plugin is inspired by flutter_downloader. thanks to hung duy ha & flutter community for great plugins and inspiration.

ios integration

  • enable background mode.

appdelegate changes

the plugin supports a background isolate. in order for plugins to work, you need to adjust your appdelegate as follows:

import flutter_uploader

func registerplugins(registry: flutterpluginregistry) {
    generatedpluginregistrant.register(with: registry)
}

@uiapplicationmain
@objc class appdelegate: flutterappdelegate {
  override func application(
    _ application: uiapplication,
    didfinishlaunchingwithoptions launchoptions: [uiapplication.launchoptionskey: any]?
  ) -> bool {
    generatedpluginregistrant.register(with: self)

    swiftflutteruploaderplugin.registerplugins = registerplugins

    // any further code.
  }
}

optional configuration:

  • configure maximum number of connection per host: the plugin allows 3 simultaneous http connection per host running at a moment by default. you can change this number by adding following codes to your info.plist file.
<!-- changes this number to configure the maximum number of concurrent tasks -->
<key>fumaximumconnectionsperhost</key>
<integer>3</integer>
  • configure maximum number of concurrent upload operation: the plugin allows 3 simultaneous upload operation running at a moment by default. you can change this number by adding following codes to your info.plist file.
<!-- changes this number to configure the maximum number of concurrent tasks -->
<key>fumaximumuploadoperation</key>
<integer>3</integer>
  • configure request timeout: controls how long (in seconds) a task should wait for additional data to arrive before giving up info.plist file.
<!-- changes this number to configure the request timeout -->
<key>futimeoutinseconds</key>
<integer>3600</integer>

android integration

optional configuration:

  • configure maximum number of concurrent tasks: the plugin depends on workmanager library and workmanager depends on the number of available processor to configure the maximum number of tasks running at a moment. you can setup a fixed number for this configuration by adding following codes to your androidmanifest.xml:
 <provider
     android:name="androidx.work.impl.workmanagerinitializer"
     android:authorities="${applicationid}.workmanager-init"
     android:enabled="false"
     android:exported="false" />

 <provider
     android:name="com.bluechilli.flutteruploader.flutteruploaderinitializer"
     android:authorities="${applicationid}.flutter-upload-init"
     android:exported="false">
     <!-- changes this number to configure the maximum number of concurrent tasks -->
     <meta-data
         android:name="com.bluechilli.flutterupload.max_concurrent_tasks"
         android:value="3" />

     <!-- changes this number to configure connection timeout for the upload http request -->
     <meta-data android:name="com.bluechilli.flutteruploader.upload_connection_timeout_in_seconds" android:value="3600" />
 </provider>

usage

import package:

import 'package:flutter_uploader/flutter_uploader.dart';

configure a background isolate entry point

first, define a top-level function:

void backgroundhandler() {
  // needed so that plugin communication works.
  widgetsflutterbinding.ensureinitialized();

  // this uploader instance works within the isolate only.
  flutteruploader uploader = flutteruploader();

  // you have now access to:
  uploader.progress.listen((progress) {
    // upload progress
  });
  uploader.result.listen((result) {
    // upload results
  });
}

the backgroundhandler function needs to be either a static function or a top level function to be accessible as a flutter entry point.

once you have a function defined, configure it in your main flutteruploader object like so:

flutteruploader().setbackgroundhandler(backgroundhandler);

to see how it all works, check out the example.

create new upload task:

multipart/form-data:

final taskid = await flutteruploader().enqueue(
  multipartformdataupload(
    url: "your upload link", //required: url to upload to
    files: [fileitem(path: '/path/to/file', fieldname:"file")], // required: list of files that you want to upload
    method: uploadmethod.post, // http method  (post or put or patch)
    headers: {"apikey": "api_123456", "userkey": "userkey_123456"},
    data: {"name": "john"}, // any data you want to send in upload request
    tag: 'my tag', // custom tag which is returned in result/progress
  ),
);

binary uploads:

final taskid = await flutteruploader().enqueue(
  rawupload(
    url: "your upload link", // required: url to upload to
    path: '/path/to/file', // required: list of files that you want to upload
    method: uploadmethod.post, // http method  (post or put or patch)
    headers: {"apikey": "api_123456", "userkey": "userkey_123456"},
    tag: 'my tag', // custom tag which is returned in result/progress
  ),
);

the plugin will return a taskid which is unique for each upload. hold onto it if you in order to cancel specific uploads.

listen for upload progress

final subscription = flutteruploader().progress.listen((progress) {
  //... code to handle progress
});

listen for upload result

final subscription = flutteruploader().result.listen((result) {
    //... code to handle result
}, onerror: (ex, stacktrace) {
    // ... code to handle error
});

when tasks are cancelled, it will send on onerror handler as exception with status = cancelled

upload results are persisted by the plugin and will be submitted on each .listen.
it is advised to keep a list of processed uploads in app side and call clearuploads on the flutteruploader plugin once they can be removed.

cancel an upload task:

flutteruploader().cancel(taskid: taskid);

cancel all upload tasks:

flutteruploader().cancelall();

clear uploads

flutteruploader().clearuploads()

Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.