简体   繁体   中英

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

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": "somebody@gmail.com"}
        ],
        "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;
  }

You can use flutter_uploader .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM