我有一个方法将图像发布到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);
}
}
问题是什么?
这是因为nginx client_max_body_size
。如果其他人有这个问题,请转到/etc/nginx/nginx.conf
路径并将此行client_max_body_size 20M;
添加到http
部分。最后做service nginx reload
。