2

I am trying to upload multiple files on server in flutter but they are taking to much time to upload.I am using Dio() for uploading..is any better way to upload multiple files on server in flutter.? I am sending upload function.in which 10 files during upload takes more than 5 minutes here is my code!

   
       _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;
  }
1
  • I'm having the same issue. How to upload large files? Did you find any possible solution? please share. Aizan Commented Feb 14, 2022 at 7:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.