【问题标题】:how to post an image file in flutter without string encoding?如何在没有字符串编码的情况下在颤动中发布图像文件?
【发布时间】:2021-12-15 13:09:53
【问题描述】:

jsonEncode({ MultipartFile.fromFile(filepath, filename: 'fileName')});

我的错误- 未处理的异常:将对象转换为可编码对象失败:LinkedHashSet len:1

【问题讨论】:

  • 为什么要jsonEncodeMultipartFile类型的对象?
  • 因为 API 只接受文件类型。谢谢你的时间
  • 它到底接受什么?
  • 它是一个文件类型的图像。不是 base64 图像
  • 贴一个真实的例子 json 应该是什么样子,“带有文件类型的图像”并没有真正说明太多

标签: json flutter file dart dart-pub


【解决方案1】:

试试这个:

import 'dart:io';
import 'dart:convert';

File imageFile = new File(imageFilePath);
List<int> imageBytes = imageFile.readAsBytesSync();
String base64Image = BASE64.encode(imageBytes);

【讨论】:

    【解决方案2】:

    发布没有base64编码的图片

    final data = FormData.fromMap({
      'image_media': await MultipartFile.fromFile(
         imagePath,
          filename: filename),
    });
    try {
      final response =
          await this._dio.post('url', data: data);
      return response;}
    

    【讨论】:

      猜你喜欢
      • 2019-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-17
      相关资源
      最近更新 更多