api - 有没有更好的方法在 Flutter 中的服务器上上传多个大文件
问题描述
我正在尝试在颤动的服务器上上传多个文件,但它们需要很长时间才能上传。我正在使用 Dio() 进行上传..有没有更好的方法在颤动的服务器上上传多个文件。?我正在发送上传功能。其中上传期间的 10 个文件需要 5 分钟以上这是我的代码!
_upLoadFiles() async {
List <FileWithComment> uploadControls = preUpload();
var token = await _getToken();
print("Token: $token");
if (files) {
try {
final result = await InternetAddress.lookup("google.com");
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
FormData data = FormData.fromMap({
"jwt": token,
"comment": [],
"directory": widget.dName,
"directory_id": widget.folderId,
"case_id": widget.case_id,
"media_files": await _mediaFileList(),
});
await Dio()
.post(
"${MyFlutterApp.baseUrl}media_upload.php",
data: data,
onSendProgress: _setUploadProgress,
options: Options(
contentType: "multipart/form-data",
),
)
.then((res) => (res.data))
.then((d) => json.decode(d))
.then((res) => postUpload(res));
}
} on SocketException catch (_) {
_saveLocal(uploadControls);
} catch (e) {
if (e.toString().contains("Cannot retrieve length of file")) {
_showSnackBar("Cannot Upload File Try Again Later", Color(0xffDC0000), Colors.white);
}
}
} else {
print("${widget.dName}");
try {
final result = await InternetAddress.lookup("google.com");
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
FormData data = FormData.fromMap({
"jwt": token,
"directory": widget.dName,
"directory_id": widget.folderId,
"case_id": widget.case_id,
"comment": list.map((filewithcomment) => filewithcomment.comment).toList(),
"media_files": await _mediaFileList(),
"f_location": list.map((filewithcomment) => filewithcomment.location).toList(),
});
await Dio()
.post("${MyFlutterApp.baseUrl}media_upload.php",
data: data,
onSendProgress: _setUploadProgress,
options: Options(
contentType: "multipart/form-data",
))
.then((res) {
return res.data;
})
.then((d) => json.decode(d))
.then((res) => postUpload(res));
}
} on SocketException catch (_) {
_saveLocal(uploadControls);
} catch (e) {
print(e);
if (e.toString().contains("Cannot retrieve length of file")) {
_showSnackBar("Cannot Upload File Try Again Later", Color(0xffDC0000), Colors.white);
}
}
}
}
This is mediafileList()..May be there is issue in these lines of code
Future<List<MultipartFile>> _mediaFileList() async {
Completer complete = Completer<List<MultipartFile>>();
List<MultipartFile> filesList = [];
for (int index = 0; index < list.length; index++) {
if (list[index].file is File) {
var file = list[index].file;
filesList.add(await MultipartFile.fromFile(file.path, filename: file.path.split('/').last));
}
if (list[index].file is String) {
var file = File(list[index].file);
filesList.add(await MultipartFile.fromFile(
file.path, filename: file.path.split('/').last));
}
if (index == list.length - 1) complete.complete(filesList);
}
return complete.future;
}
解决方案
推荐阅读
- scala - 如何在 Spark 和 Scala 中动态构建模型类
- javascript - Gulp 修复生成文件的导入
- android - 即使我在android登录表单中输入了错误的信用,总是得到响应匹配(200)并登录?
- go - 是否有可能从 err interface Go 获取额外的数据?
- c++ - C++ 代码中的错误:在函数“int main(int, char**)”中:
- django - 分叉 django-oscar 视图显示错误
- bluetooth-lowenergy - 有没有办法断开活动的蓝牙设备(传统连接),同时保持 BLE 连接与 bluez 处于活动状态?
- android-studio - Android Studio 说 FAILURE: Build failed with an exception after update to 3.6.2
- c# - 如何自动将外键属性映射到 EF6 中的数据库
- python - 使用 TSNE 进行降维。为什么 3D 图形不起作用?