上传图片时Flutter Dio包异常413
Flutter Dio package Exception 413 when uploading an image
我正在尝试通过 Dio 包使用 Form 数据上传 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,由于我从phone和postman上传了不同大小的不同图片,所以花了我花了很多时间来弄清楚。我联系了DevOps,问题解决了。
我正在尝试通过 Dio 包使用 Form 数据上传 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,由于我从phone和postman上传了不同大小的不同图片,所以花了我花了很多时间来弄清楚。我联系了DevOps,问题解决了。