Flutter如何将多个文件发送到http发布 [英] Flutter how to send multiple files to http post

查看:135
本文介绍了Flutter如何将多个文件发送到http发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将两个文件发送到http post

I'd like to send two files to http post

卷曲看起来像这样

curl -X POST "https://api-us.faceplusplus.com/facepp/v3/compare" \
-F "api_key=<api_key>" \
-F "api_secret=<api_secret>" \
-F "image_file1 =file1" \
-F "image_file1 =file2"

我这样尝试过。

   File first;
   File second;
      var uri = Uri.parse('https://api-us.faceplusplus.com/facepp/v3/compare');
      var request = new http.MultipartRequest("POST", uri);
       request.fields['api_key'] = apiKey;
       request.fields['api_secret'] = apiSecret;
       request.files.add(await http.MultipartFile.fromPath('image_file1', first.path, contentType: new MediaType('application', 'x-tar')));
       request.files.add(await http.MultipartFile.fromPath('image_file2', second.path, contentType: new MediaType('application', 'x-tar')));
       var response = await request.send();
       print(response);

但它会返回此


NoSuchMethodError:类'String'没有实例获取方法'path'。

NoSuchMethodError: Class 'String' has no instance getter 'path'.

如何正确发送这些? / p>

How can I send these properly?

推荐答案

它看起来不像 first second 实际上是 File s。当它们绝对是文件时,如下面的示例所示,我得到401(按预期,因为我有一个虚拟的api密钥)。

It doesn't look like first and second are actually Files. When they are definitely files, as in the following example, I get 401 (as expected, as I have a dummy api key).

main() async {
  File first = File('pubspec.yaml');
  File second = File('analysis_options.yaml');
  Uri uri = Uri.parse('https://api-us.faceplusplus.com/facepp/v3/compare');
  http.MultipartRequest request = new http.MultipartRequest('POST', uri);
  request.fields['api_key'] = 'apiKey';
  request.fields['api_secret'] = 'apiSecret';
  request.files.add(await http.MultipartFile.fromPath('image_file1', first.path,
      contentType: new MediaType('application', 'x-tar')));
  request.files.add(await http.MultipartFile.fromPath(
      'image_file2', second.path,
      contentType: new MediaType('application', 'x-tar')));
  http.StreamedResponse response = await request.send();
  print(response.statusCode);
}

这篇关于Flutter如何将多个文件发送到http发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆