【问题标题】:Flutter camera image to api将相机图像抖动到 api
【发布时间】:2019-11-30 09:43:13
【问题描述】:

我正在调用 Flutter Camera 包,拍照并使用 pathProvider 将其临时保存在我的应用程序中。现在我想使用 Dio 包将此图像上传到 api。我的路径提供者能够给我一个带有图像路径的字符串。通过这条路径,我可以使用 Flutter 中的 Image 小部件显示图像。但是我无法将其发送到 api。

processImage(file) async {
    print(file);
    // /var/mobile/Containers/Data/Application/7915E945-4582-4586-8062-F73537283283/Library/Caches/2019-07-22 13:35:36.243335.png

var newfile = File(file);

Dio dio = new Dio();

FormData formData =
    new FormData.from({"size": "auto", "image_file": newfile});

try {
  final response = await dio.post(
    "YYYYYYYYYYYY",
    data: formData,
    options: Options(
      headers: {'X-Api-Key': 'XXXXXXX'},
    ),
    onSendProgress: (int sent, int total) {
      print("send $sent / $total");
    },
    onReceiveProgress: (int sent, int total) {
      print("receive $sent / $total");
    },
  );
} on DioError catch (e) {
  // The request was made and the server responded with a status code
  // that falls out of the range of 2xx and is also not 304.
  print(e);
}

// print(response);
  }

【问题讨论】:

  • not able to send it to an api 是什么意思?有什么错误吗?
  • 我得到一个 400 意味着“输入文件无法处理”。 image_file 需要是一个字符串($binary)
  • 服务器期待什么?是否期待bas64 字符串?
  • 不只是 FormData 中的文件。我想我可以将其转换为 base64 并将其发送到那里。但我认为这不是必需的

标签: ios flutter dart


【解决方案1】:

Dio 库有一个名为 UploadFileInfo 的类,您可以将其用于您的案例:

FormData formData =
    new FormData.from({
            "size": "auto", 
            "image_file": UploadFileInfo(newfile, "file_name.txt")},
        );

【讨论】:

    猜你喜欢
    • 2020-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多