[英]WSDL file upload in flutter?
我對 flutter 相當陌生。 我正在尋找有關如何正確使用顫振的 HHTP 庫的一些指導。
我的挑戰是使用 WSDL 服務來上傳圖片。 這是兩個代碼(顫振與 Java),它們執行相同的 function 和相同的 WSDL。 Java 有效!
我使用以下示例構建了我的 flutter 代碼:如何在 Flutter 中上傳圖像。 如何在 Flutter 中上傳圖片?
但是我下面的 flutter 代碼返回服務器錯誤 500:請參閱下面的屏幕截圖以獲得參考和清晰度。
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;
}
}
這是與相同的 WSDL 一起使用的 Java 代碼示例:
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
如何使用上面的 HTTP 庫獲得上面的 flutter 代碼才能工作? HHTP 庫是使用 WSDL 的正確庫嗎?
感謝您的指導。
呆在家里。 注意安全!
目前,在 Dart 中,您正在使用多部分請求,在 Java 中,您正在發送 stream。 我建議也嘗試發送 stream。 嘗試通過使用奇妙的dio庫來做到這一點。
那里有一個發送它的例子:
// 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
},
),
);
如果您在評論中需要更多內容,請告訴我。 希望它有效。
此代碼擴展了上面的 Lorenzo 答案。 這段代碼對我有用,希望對其他人有所幫助。
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.