如何解决使用具有大量数据的Dio发送请求时,Flutter应用程序将关闭 编辑
我正在尝试使用 FormData 和 Dio 发送大图像,服务器端是用 NodeJS
编写的每当我发送 1 MB 的图片时,它就可以正常工作,但是我尝试发送 17 MB 大小的图像,但应用程序对此有所滞后一会儿,然后退出
这是颤动代码
Future<Uint8List> _convertImageToUint8List(final String imagePath) async {
ByteData imageByteDate = await rootBundle.load(imagePath);
ByteBuffer imageBuffer = imageByteDate.buffer;
return imageBuffer.asUint8List();
}
void _testUploadingImageWithDio(
{final String ip,final String port,final String api,final String imagePath}) async {
Dio dio = new Dio();
Uint8List imageUint8List = await _convertImageToUint8List(imagePath);
FormData formData = new FormData.fromMap({
'image': imageUint8List,});
String url = 'http://$ip:$port/$api';
print('URL: $url}');
var response = await dio.post(url,data: formData);
print('response: $response');
}
除了我尝试了另一种在 MultipartRequest 中的课程外,但图像的大小增加了一倍,我的意思是 17 MB 的图像通过 32 MB 大小,并且图像已损坏。
这是代码
void _testUploadingImageWithMultiPartRequest(
{final String ip,final String imagePath}) async {
// Constructing the request
Uri postUri = Uri.parse('http://$ip:$port/$api');
print('URI ${postUri.toString()}');
http.MultipartRequest request = new http.MultipartRequest("POST",postUri);
// request.fields['user'] = 'someone@somewhere.com';
// Loading the image from image path using root bundle
// convert it to buffer and uint8 list
// append it to the request
Uint8List imageUint8List = await _convertImageToUint8List(imagePath);
http.MultipartFile imageToBeSent = new http.MultipartFile.fromBytes(
'image',imageUint8List,contentType: new MediaType('image','png'));
request.files.add(imageToBeSent);
// Sending the request
request.send().then((response) {
if (response.statusCode == 200) print("Uploaded!");
}).catchError((onError) {
print(onError);
});
}
对于服务器端应用程序,我正在执行以下操作
const upload = multer({storage: storage,fileFilter: fileFilter,limits: {fieldSize: 200 * 1024 * 1024}});
app.post('/upload',upload.single('image'),(req,res) => {
const buffer = Buffer.from(req.body['image']);
fs.writeFileSync('./some_image',buffer);
try {
return res.status(200).json({
message: 'File uploded successfully'
});
} catch (error) {
console.error(error);
}
});
编辑
对于17 MB的图像
对于损坏的服务器,在服务器端
对于效果不错的小图像
解决方法
此服务器配置是否相关?
例如,对于Nginx,有一个配置:client_max_body_size 50M;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。