[英]how to upload file to the server in the background in Dart Flutter?
我正在使用 multipart/form-data 在我的應用程序中使用 POST API 將文件上傳到服務器(使用dio包)。 問題是應用程序必須在后台上傳文件(即使用戶退出應用程序)。 我怎樣才能做到這一點? 將感謝每一個回應!
這就是我將文件上傳到服務器的方式
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;
}
您可以使用flutter_uploader 。
本插件基於Android中的WorkManager和iOS中的NSURLSessionUploadTask實現后台運行上傳任務。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.