Flutter HTTP文件发布示例:图像

问题描述 投票:1回答:1
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,
        }
    );

我想使用此方法将图像文件发布到服务器。但我不知道如何。谁能帮我 ?

image file http flutter post
1个回答
0
投票

对于图像上传,您还可以使用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

© www.soinside.com 2019 - 2024. All rights reserved.