Flutter:-HTTP文件发布示例:图像 [英] Flutter :- HTTP File Post Example: Image

查看:47
本文介绍了Flutter:-HTTP文件发布示例:图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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,
        }
    );

我想用这种方法将图像文件发布到服务器.但是我不知道如何.谁能帮我 ?

I want to post image file to server with this method. But I Don't Know How. Can Anyone Help Me ?

推荐答案

对于图像上传,您还可以使用 Dio 库将具有请求参数的图像发布到服务器上请检查以下示例.

For the image upload you can also use the Dio library to post image on server with requested parameters.Please check the below example of it.

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内部使用了该库,

Inside the pubspec.yaml used this library,

dio:^ 3.0.9

有关该库的信息,您需要检查此链接单击

For the info about the library , you need to check this link Click

这篇关于Flutter:-HTTP文件发布示例:图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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