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;
}
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.
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.
You can use flutter_uploader.
This plugin is based on WorkManager in Android and NSURLSessionUploadTask in iOS to run upload task in background mode.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With