首页 > 解决方案 > 有没有更好的方法在 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;
  }

标签: apiflutterservermultiple-file-upload

解决方案


推荐阅读