Search code examples
filehttpflutteruploadhttpurlconnection

WSDL file upload in flutter?


I am fairly new to flutter. I am looking for some guidance on how to use flutter’s HHTP library correctly.

My challenge is consuming a WSDL service to upload a picture. Here are two codes (flutter vs. Java) that perform the same function with the same WSDL. Java works!

I build my flutter code using this example: How to upload image in Flutter. How to upload image in Flutter?

But my flutter code below returns server error 500: See screen shots below for reference and clarity.

Future<bool> sentPhotoTransaction () async {
 // URL includes two parameter plus the image file stream.
 String cIPhotoPath = "/storage/emulated/0/Android/data/com.saleson24.saleson24/files/Pictures/scaled_IMG_20200414_161101.jpg";
 String urlString = "http://pro.test.com/ImgHandler.WCFHost/FileManagerService.svc/UploadFile?MobID=20A47616&Sig=b6e61d4e3ee38";
Io.File imageFile;
 imageFile = new Io.File(cIPhotoPath);
 // ***************** create multipart request for POST *****************
 var request = http.MultipartRequest("POST", Uri.parse(urlString));
 // ***************** create multipart using filepath, string or bytes *****************
 var picture = await http.MultipartFile.fromPath("stream", imageFile.path);
 // ***************** add multipart for the picture to request *****************
 request.files.add(picture);
 try {
   var response = await request.send();
   if (response.statusCode == 200) {
     print("Success");
     return true;
   } else {
     print("POST request did not worked");
     return false;
   }
 } catch(e) {
   print(e.toString());
   return false;
 }
}

enter image description here enter image description here

Here is a Java code example that worked with the same WSDL:

    java.net.URL url = new URL(urlString);
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setDoOutput(true);    //Allow Outputs
    urlConnection.setUseCaches(false);  //Don't use a cached Copy
    urlConnection.setRequestMethod("POST");
    Bitmap full_image = BitmapFactory.decodeFile(filepath);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    full_image.compress(Bitmap.CompressFormat.JPEG, IMAGE_QUALITY, stream); // Convert stream.
    byte[] byteArray = stream.toByteArray();
    DataOutputStream dos = new DataOutputStream(urlConnection.getOutputStream());
    dos.write(byteArray);
    dos.flush();
    dos.close();                 // END POST

How do I get the flutter code above using the HTTP library above to work? Is the HHTP Library the correct library to consume WSDL?

Your guidance is appreciated.
Stay @ home. Be safe!


Solution

  • This code expands on Lorenzo answer above. This code worked for me, hope that helps others.

        String photoPath = "";      // Your photo location path.
        Io.File file = new Io.File(photoPath);
        var dio = Dio();
        // ***************** Transfer File *****************
        try {
    // Convert file to Bytes WITHOUT compression.
    //          List<int> postData = await file.readAsBytes();                      
    // Convert file to Bytes WITH compression.
              List<int> postData = await compressImageFileAndReturnList(file);      
          var response = await dio.post(urlString,
              data: Stream.fromIterable(postData.map((e) => [e])),
              options: Options(
                  followRedirects: false,
                  headers: {
                    Headers.contentLengthHeader: postData.length, // set content-length
                  }
              )
          );
          if (response.statusCode == 200) {
            print("Success");
            return true;
          } else {
            print("POST request did not worked");
            return false;
          }
        } catch(e) {
          print(e.toString());
          return false;
        }