0

this is my upload function

void upload() async {
    print("inside upload image");
    if (file == null) return;
    String base64 = base64Encode(file.readAsBytesSync());
    String imagename = file.path.split("/").last;
    dynamic data = {"imagename": imagename, "image64": base64};
  

    try {
      response1 = Dio().post(
        "***********************",
        data: data,
        options: Options(
            followRedirects: false,
            validateStatus: (status) {
              return status < 500;
            }),
      );
     
    } on DioError catch (e, s) {
      print("what happend  when upload image   ? :    " + e.toString());
    }
   

   
  }

when try to upload an image in the app the image showed successfully in the app < but when trying to send it to the api i get this error

enter image description here

SH EB
  • 61
  • 8

1 Answers1

0
String imageName = file.path.split('/').last;

    FormData data = FormData.fromMap({
      "imagename": await MultipartFile.fromFile(
        file.path, filename: imageName),
    });

    Dio dio = new Dio();

    dio.options.connectTimeout =(60*1000); 
    dio.options.receiveTimeout= (60*1000);


    dio.post("*******************", data: data,
        options: Options(
            followRedirects:false,
            validateStatus: (status)=> return status < 500,
        )).then((response) {
      
      if(response.statusCode==200){
        // response good
      }
      else //Something went wrong

If you have token(bearer token) then pass it on header

SH EB
  • 61
  • 8
Maz341
  • 2,232
  • 15
  • 20