Flutter :- HTTP File Post Example: Image

后端 未结 1 1618
無奈伤痛
無奈伤痛 2021-01-27 03:33
Future userPasswordUpdate() async {

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


        
相关标签:
1条回答
  • 2021-01-27 04:33

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

    Inside the pubspec.yaml used this library,

    dio: ^3.0.9

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

    0 讨论(0)
提交回复
热议问题