简体   繁体   中英

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

在此处输入图像描述 在此处输入图像描述

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!

In Dart, at the moment, you're using a multipart request, in Java you're sending a stream. I suggest to try to send a stream too. Try to do it by using the fantastic dio library.

There is an example in there for sending it:

// Binary data
List<int> postData = <int>[...];
await dio.post(
  url,
  data: Stream.fromIterable(postData.map((e) => [e])), //create a Stream<List<int>>
  options: Options(
    headers: {
      Headers.contentLengthHeader: postData.length, // set content-length
    },
  ),
);

Let me know if you need something more in the comments. Hope it works.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM