如何解决Flutter Dio 包上传图片时出现异常 413
我正在尝试通过 Dio 包使用表单数据上传 jpg 图像,请求在 Postman 上正常工作,所以我认为问题出在我的代码或我试图上传的图像中。
至于图像,我有一个应用了一些编辑的位图,我将此 btm 转换为 Uint8List,然后将其编码为 jpg 文件,我使用位图插件来做到这一点。
final directory = await getApplicationDocumentsDirectory();
File image = await File('${directory.path}/image.jpg').create();
await image.writeAsBytes(widget.editedBitmap.buildHeaded());
然后我通过解码得到图像的宽度和高度
var decodedImage = await decodeImageFromList(imageBytes);
print(decodedImage.width);
print(decodedImage.height);
然后我创建我的表单数据
FormData formData = new FormData.fromMap({
'title': titleController.text,'description': descriptionController.text,'is_public': privacy == 'Public' ? true : false,'photo_width': decodedImage.width,'photo_height': decodedImage.height,'media_file': await MultipartFile.fromFile(
image.path,filename: image.path.split("/").last,contentType: new MediaType("image","jpeg"),),});
然后我创建我的 dio 实例并配置选项
var dio = new Dio();
dio.options.baseUrl = globals.HttpSingleton().getBaseUrl();
dio.options.connectTimeout = 5000; //5s
dio.options.receiveTimeout = 3000;
dio.options.headers = {
HttpHeaders.acceptHeader: '*/*',HttpHeaders.authorizationHeader: 'Bearer ' + globals.accessToken,HttpHeaders.contentTypeHeader: 'multipart/form-data'
};
这是我的要求
Response response;
try {
response = await dio.post(
'/photos/upload',data: formData,onSendProgress: (int sent,int total) {
print('$sent $total');
},);
} on DioError catch (e) {
print(e.response.data);
}
这是我得到的回应 (e)
I/flutter (12154): <html>
I/flutter (12154): <head><title>413 Request Entity Too Large</title></head>
I/flutter (12154): <body>
I/flutter (12154): <center><h1>413 Request Entity Too Large</h1></center>
I/flutter (12154): <hr><center>nginx/1.18.0 (Ubuntu)</center>
I/flutter (12154): </body>
I/flutter (12154): </html>
当我搜索这个时,它说当我的请求对于服务器来说太大时会发生 413,但是,使用邮递员,并且在上传更大尺寸的图像时,它仍然有效,这对我来说没有意义,任何知道可能是什么问题吗?
如果需要任何进一步的信息,请告诉我。
解决方法
原来是在部署服务器的时候,如果不指定,默认上传限制设置为1MB,而且由于我从手机和邮递员那里上传了不同大小的不同图片,花了我很多时间想办法。我联系了 DevOps,问题得到了解决。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。