2

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?

artick
  • 175
  • 1
  • 13

1 Answers1

2

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 .

artick
  • 175
  • 1
  • 13