【问题标题】:Flutter SocketException problem with large images on iosios上大图出现Flutter SocketException问题
【发布时间】:2019-03-09 15:57:33
【问题描述】:

我有一种将图像发布到 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);
}
}

有什么问题?

【问题讨论】:

  • 似乎是服务器端问题。我建议你看看 API 的大小限制
  • @MazinIbrahim 我只是检查了一下,发现它适用于大尺寸图像的 android 设备。它只是在 ios 端。

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2020-10-23
    • 2020-08-26
    • 2019-05-02
    • 2020-09-05
    • 2021-10-21
    • 1970-01-01
    • 2021-07-18
    • 2021-07-31
    • 2022-06-21
    相关资源
    最近更新 更多