【问题标题】:Flutter :- HTTP File Post Example: ImageFlutter :- HTTP 文件发布示例:图片
【发布时间】:2020-07-28 01:14:12
【问题描述】:
Future userPasswordUpdate() async {

    String passwordU = password.text;
    String confirmPasswordU = confirmPassword.text;
    String oldPasswordU = oldPassword.text;

    var url = 'url';

    var response = await http.put(url,
        headers: {
          'Accept': 'application/json'
        },
        body: {
          "password": passwordU,
          "confirmPass": confirmPasswordU,
          "oldpassword": oldPasswordU,
        }
    );

我想用这种方法将图像文件发布到服务器。但我不知道怎么做。谁能帮我 ?

【问题讨论】:

  • 嗨@Magatha,请检查以下解决方案,如果有问题请告诉我

标签: image file http flutter post


【解决方案1】:

对于图片上传,您还可以使用 Dio 库在服务器上发布带有请求参数的图片。请查看以下示例。

Dio dio = new Dio(); // with default Options

// Set default configs
    dio.options.baseUrl = BASE_URL;
    dio.options.connectTimeout = 5000; //5s
    dio.options.receiveTimeout = 3000;
    dio.options.headers[HEADER_AUTH_TOKEN_KEY] = HEADER_AUTH_TOKEN_VALUE;
    dio.options.headers[HEADER_VERSION_KEY] = HEADER_VERSION_VALUE;



    FormData formData = new FormData.fromMap({
      "password": passwordU,
      "confirmPass": confirmPasswordU,
      "oldpassword": oldPasswordU,


      "YOUR_IMAGE_PARAMETER_NAME": await MultipartFile.fromFile(imageFile.path,filename: imageFile.path.split("/").last),

    });

    var response = await dio.post(REGISTRATION_URL, data: formData);

    if (response.statusCode == 200) {
      apiResponse.onSuccess(response.toString(), eventType);
      print("Image Uploaded");
    } else {
      apiResponse.onError('Failed to load post');
      print("Upload Failed");
    }
  }

pubspec.yaml里面用到了这个库,

dio:^3.0.9

有关图书馆的信息,您需要查看此链接Click

【讨论】:

  • 我仍然遇到类似未处理异常的错误:DioError [DioErrorType.RESPONSE]:Http 状态错误 [422]
  • 我已经更新了解决方案,您需要同时注意 BASE_URLREGISTRATION_URL 就像在您的基本网址中只有像 http/ 这样的网址/www.xxxxxx.com 并且在您的 REGISTRATION_URL 中还有其他名称,例如“updateProfile.php”
  • 谢谢,我认为它成功了。非常感谢你
  • @Magtha,很高兴为您提供帮助..祝您编码愉快
猜你喜欢
  • 2021-09-23
  • 1970-01-01
  • 1970-01-01
  • 2014-04-22
  • 1970-01-01
  • 2019-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多