How can I solve FileSystemException: Cannot retrieve length of file, path ='...' (OS Error: No such file or directory, errno = 2)

8,473

In Flutter, you can't access files directly from the project. You need to add them to an assets folder (typically assets) and also to pubspec.yaml. Then, instead of using File to read them, you use rootBundle (or one of the Asset classes).

var multipartFile = http.MultipartFile.fromBytes(
  'file',
  (await rootBundle.load('assets/anatomy.jpg')).buffer.asUint8List(),
  filename: 'anatomy.jpg', // use the real name if available, or omit
  contentType: MediaType('image', 'jpg'),
);

request.files.add(multipartFile);

While testing your API, you may find it easier to just create a Dart command line project, where you do have access to Files in the project folders.

Share:
8,473
DCodes
Author by

DCodes

Happy Coding ^^

Updated on December 17, 2022

Comments

  • DCodes
    DCodes 6 days

    I am trying to send a user model from dart to the api where my file is set as "IFormFile" data type in my c# backend. I tried using the multipart request but all i get is the error stated , i can't understand why it cannot retrieve the length of file.

    This is my code:

    updateUser() async {
    var uri = Uri.parse('http://myIP:8070/api/Users');
    var request = http.MultipartRequest('Put', uri);
    request.fields['id']="07bb2a17-7cd5-471b-973a-4b77d239b6c3";
    request.fields['username']="beeso";
    request.fields['email']="[email protected]";
    request.fields['password']="Jake123-";
    request.fields["oldPassword"]="Jake124-";
    request.fields["gender"]="Male";
    request.fields["dateOfBirth"]=DateTime.now().toString();
    request.fields["name"]="dsjnss";
    request.fields["languages"]=["Language1","Language2"].toString();
    request.fields["nationalities"]=["Nationality1","Nationality2"].toString();
    request.fields["phoneNumber"]="70502030";
    request.fields["biography"]="jdnknkdas";
    request.fields["info"]="asndasnkdas";
    request.fields["religion"]="Christian";
    request.fields["location"]="LA";
    File imageFile = new File('Anatomy_of_a_Sunset-2.jpg');
    var stream = new http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
    var length = await imageFile.length();
    var multipartFile = new http.MultipartFile('file', stream, length,
        filename: basename(imageFile.path));
    request.files.add(multipartFile);
    Map<String, String> headers = {
      "content-type": "application/json"
    };
    
    request.headers.addAll(headers);
    http.StreamedResponse response = await request.send();
    print(response.statusCode);
    
    
    }
    

    Any help would be appreciated.

    This picture shows where my file is located

    • Richard Heap
      Richard Heap almost 3 years
      1 - your content type isn't JSON, (it's multipart), so omit that header. 2 - there's another name constructor for MultipartFile that is better when expecting a file - it will save you two or three lines. 3 - the error message is telling you that the file "Anatomy..." doesn't exist. It must be in a different folder or called a different name. Where is it?
    • DCodes
      DCodes almost 3 years
      Alright, I changed it to "content-type": "multipart/form-data" now, the Anatomy file is in the same directory as pubspec.yaml (beside it), and regarding the constructor i tried a lot and many were giving errors i didn't know how to fix that's why i sticked to it. @RichardHeap
    • Richard Heap
      Richard Heap almost 3 years
      Is this a dart app? Or a flutter app? If the latter, you can access files like that. You'd need to move it to assets and load it. As is easier to test this in a pure dart app, create a new project. If you didn't already, download the dart SDK (2.7.1) and point your ide's dart plugin to it.
    • DCodes
      DCodes almost 3 years
      It is a flutter app, i added a link to my post to see where the file is located. I also tried previously adding an assets folder that is shown in the screenshot and adding the file instead it. Following that i loaded it from the pubspec.yaml file and had it as new File('assets/Anatomy_of_a_Sunset-2.jpg'); but still the same error. @RichardHeap
  • DCodes
    DCodes almost 3 years
    I got this now :415 Unsupported Media Type client error response code indicates that the server refuses to accept the request because the payload format is in an unsupported format. I have the profilePicture as IFormFile in my c# backend
  • Richard Heap
    Richard Heap almost 3 years
    Get it working using Postman first (see stackoverflow.com/questions/46895523/… etc). Then update the question with a screenshot of the working Postman request.
  • Anirudh Sharma
    Anirudh Sharma over 2 years
    Is there Anu way I can send no file in it and still pass the file field in it.When I am passing null in the parameter it's giving me error.