Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to upload file to the server in the background in Dart Flutter?

Tags:

flutter

dart

I'm using multipart/form-data to upload files to the server using POST API in my application (using dio package). The problem is application must upload files in the background (even when the user quits the application). How can I achieve this? Will appreciate every response!

This is how I'm uploading file to the server

Future<bool> upload(File file) async {
    bool isSuccessfull = false;
    var dio = Dio();
    dio.options.baseUrl = "$baseUrl";
    dio.interceptors.add(LogInterceptor(
        requestBody: true,
        request: true,
        responseBody: true));

    try {
      FormData formData = FormData.from({
        "iframeKey": "foofoo",
        "apikey": "foo",
        "secret": "foo",
        "fields": [
          {"key": "first_name", "value": "videoupload"},
          {"key": "larst_name", "value": "videoupload"},
          {"key": "test", "value": "videoupload"},
          {"key": "checkboxtest", "value": "true"},
          {"key": "email_address", "value": "[email protected]"}
        ],
        "file": [
          new UploadFileInfo(new File(file.path), basename(file.path)),          
        ],
      });

      Response response;
      response = await dio.post("/submit",
          data: formData,
          onSendProgress: showDownloadProgress,
          options: new Options(
            connectTimeout: 100000,
            receiveTimeout: 100000,
            contentType: ContentType.parse("application/x-www-form-urlencoded"),
          ));
      if (response.statusCode == 200) {
        isSuccessfull = true;
      }
    } on DioError catch (e) {
       print(e.message);
    }
    return isSuccessfull;
  }
like image 489
Dudona Avatar asked Apr 18 '19 13:04

Dudona


People also ask

How do you upload files to a server on Flutter?

To upload images and files to a server in Flutter we have multiple methods we can upload images to server by Using MultipartRequest class, we cal also use using http Plugin And Dio Plugins Here is the example given below.

How do I upload a video in the background Flutter?

We'll use the flutter_uploader plugin to perform the background uploads. It's based on WorkManager in Android and NSURLSessionUploadTask in iOS, which are the platform specific ways to perform upload tasks that continue in the background, even after exiting the app.


1 Answers

You can use flutter_uploader.

This plugin is based on WorkManager in Android and NSURLSessionUploadTask in iOS to run upload task in background mode.

like image 170
John Joe Avatar answered Oct 13 '22 06:10

John Joe