Flutter SocketException problem with large images on ios

1,908

It was because of nginx client_max_body_size. If anyone else has this problem, go to /etc/nginx/nginx.conf path and add this line client_max_body_size 20M; to the http portion. and at the end do service nginx reload .

Share:
1,908
artick
Author by

artick

Updated on December 09, 2022

Comments

  • artick
    artick 11 months

    I have a method for posting image to a django-rest api. It works good for small size images. But when it comes to 900 KB or more (Like ios images), it takes some time and gives me this error(Also this problme is just occur when I use an ios device. No problem with android) :

    SocketException: OS Error: Connection reset by peer, errno = 54, address = 192.168.1.1, port = 52842

    Here is the code:

    postImage(
        BuildContext context, String name, String description, var image) async {
      SharedPreferences preferences = await SharedPreferences.getInstance();
      final url = "http://192.168.1.1/posts/";
      final uri = Uri.parse(url);
      final subject = BehaviorSubject<Map<String, dynamic>>();
      Map<String, dynamic> responseDetail;
      var response;
    
      var request = http.MultipartRequest('POST', uri);
      request.headers[HttpHeaders.authorizationHeader] =
          'Token ${preferences.getString('Key')}';
      request.headers[HttpHeaders.acceptHeader] = 'application/json';
      request.fields['name'] = name;
      request.fields['description'] = description;
      if (image != null) {
        var length = await image.length();
        var stream = http.ByteStream(DelegatingStream.typed(image.openRead()));
        request.files.add(http.MultipartFile('image', stream, length,
            filename: basename(image.path),));
      } else {
        request.fields['image'] = '';
      }
      try {
        response = await request.send();
        if (response.statusCode != 201) {
          response.stream.transform(utf8.decoder).listen((value) {
        responseDetail = json.decode(value);
      }, onDone: () {
        subject.add(responseDetail);
        subject.close();
      });
      return subject.share();
    }
    return response.statusCode;
      } catch (e) {
    print(e);
    }
    }
    

    What is the problem?

    • Mazin Ibrahim
      Mazin Ibrahim over 4 years
      Seems like a server-side issue. I advise you to look at the size constraints of the API
    • artick
      artick over 4 years
      @MazinIbrahim I just check it and find out it works on android device for large size images well. It's just on ios side.