Home > Software engineering >  How can I solve FileSystemException: Cannot retrieve length of file, path ='...'
How can I solve FileSystemException: Cannot retrieve length of file, path ='...'

Time:04-22

    E/flutter ( 8324): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: FileSystemException: Cannot retrieve length of file, path = 'File: '/data/user/0/com.example.upload/cache/file_picker/download (1).jpeg'' (OS Error: No such file or directory, errno = 2)
E/flutter ( 8324): #0      _File.length.<anonymous closure> (dart:io/file_impl.dart:366:9)
E/flutter ( 8324): #1      _rootRunUnary (dart:async/zone.dart:1434:47)
E/flutter ( 8324): #2      _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter ( 8324): <asynchronous suspension>
E/flutter ( 8324): #3      uploadmultipleimage (package:upload/image_upload.dart:42:18)
E/flutter ( 8324): <asynchronous suspension>
E/flutter ( 8324): #4      WriteSQLdataState.build.<anonymous closure> (package:upload/upload_screen.dart:212:33)
E/flutter ( 8324): <asynchronous suspension>
E/flutter ( 8324): 



    Future uploadImg(List<File> img)async{
  final request = http.MultipartRequest("POST", Uri.parse("http://192.168.94.221/easy/uploadfile.php"));
  for (final file in img) {
    File imageFile = File(img.toString());
    var stream =
    http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
    var length = await imageFile.length();
    print(length);
    var multipartFile = http.MultipartFile("file",stream, length, filename: basename(file.path));
    request.files.add(multipartFile);
    print(file.path);
    var myRequest = await request.send();
    var response = await http.Response.fromStream(myRequest);
    if(myRequest.statusCode == 200){
      //return jsonDecode(response.body);
      print('upload sucess');
    }else{
      print("Error ${myRequest.statusCode}");
    }

  }
  //request.headers[HttpHeaders.authorizationHeader] = '';
  // request.headers[HttpHeaders.acceptHeader] = 'application/json';

  final response = await request.send();
  if(response.statusCode == 200)
  {
    print("image sent${response.statusCode}");
  }else{
    print("ERROR");
  }
}

im trying to upload multiple images to the server using http, i can upload one picture at a time but i need to upload multiple files of images all at the same time here is my function:

CodePudding user response:

Problem:

The problem is that you're creating a new file in the line below but you're passing the string representation of the file instead of the file's path into the new file object.

    File imageFile = File(img.toString());

That's why the path in the error message is 'File: '/data/user/0/com.example.upload/cache/file_picker/download (1).jpeg' and it can not be found.

Solution:

There are two ways to go about solving this:

  1. Create the new File object using by using the file path instead of the string presentation.

    Change this line:

    File imageFile = File(img.toString());
    

    to this:

    File imageFile = File(file.path);
    
  2. Use the file object in the loop directly without creating a new File object. Update your for-loop to this:

    for (final file in img) {
        var stream =
        http.ByteStream(DelegatingStream.typed(file.openRead()));
        var length = await file.length();
        print(length);
        var multipartFile = http.MultipartFile("file",stream, length, filename: basename(file.path));
        request.files.add(multipartFile);
        print(file.path);
        var myRequest = await request.send();
        var response = await http.Response.fromStream(myRequest);
        if(myRequest.statusCode == 200){
          //return jsonDecode(response.body);
          print('upload sucess');
        }else{
          print("Error ${myRequest.statusCode}");
        }
    
    }
    
  • Related