使用具有大量数据的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的图像

First big image hex code

对于损坏的服务器,在服务器端

Corrupted image on server side

对于效果不错的小图像

Second image hex code

解决方法

此服务器配置是否相关?

例如,对于Nginx,有一个配置:client_max_body_size 50M;

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...