在iOS上使用大型图像出现Flutter SocketException问题

时间:2019-03-09 15:57:33

标签: django-rest-framework flutter flutter-dependencies

我有一种将图像发布到Django-rest api的方法。 它适用于小尺寸图像。但是,当涉及到900 KB或更多(例如ios图像)时,它会花费一些时间,并给我这个错误(同样的问题只是在使用ios设备时发生。Android没问题):

  

SocketException:操作系统错误:对等重置连接,errno = 54   地址= 192.168.1.1,端口= 52842

代码如下:

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);
}
}

出什么问题了?

1 个答案:

答案 0 :(得分:0)

这是因为nginx client_max_body_size。 如果还有其他人遇到此问题,请转到/etc/nginx/nginx.conf路径,并将此行client_max_body_size 20M;添加到http部分。最后做service nginx reload