uploadFile method
Implementation
Future<String> uploadFile(file) async {
// print('//// uploadFile ///');
String fileName = path.basename(file.path);
String fileName1 = fileName.replaceAll(".jpg", "");
List<int> bytes = await file.readAsBytes();
// Encode the bytes as Base64
String base64File = base64Encode(bytes);
// print(base64File);
//print(base64File.length);
//print('fileName=$fileName');
var postData = {
"image": base64File,
"name": fileName1,
};
//log('');
//log('//// $base64File//');
// var url = Uri.https(baseUrl1, '/api/sdk/v1/upload');
try {
var url = Uri.https(baseUrl1, '/sdk-api/v1/upload');
///https://test.instafido.com/api/sdk/v1/upload
// Await the http get response, then decode the json-formatted response.
// print('upload start');
var response = await http.post(url,
body: postData, headers: {"document-access-token": accessToken});
// print("response=${response}");
//print("response.bodyr=${response.statusCode}");
// print('response=${response.body}');
// print("statusCode=${response.statusCode}");
if (response.statusCode == 200) {
// https://ifdimages.s3.ap-south-1.amazonaws.com/CAP2535183295181659362_cropped3236357715881767975.jpg
// String value = response.body.toString().replaceAll(
// "https://ifdimages.s3.ap-south-1.amazonaws.com/", "");
// String value1 = value.replaceAll(".jpg.jpg", ".jpg");
// print("value=$value");
String value1=response.body.toString();
String value3 = value1.substring(value1.lastIndexOf('/') + 1);
//String url = "/$value1";
print("value3=$value3");
return value3;
} else {
// print('response=${response.body.toString()}');
//return "";
showErrorToast(
"Something went wrong,please try ",
);
throw Exception();
}
} on TimeoutException catch (e) {
// print('Timeout Error: $e');
showErrorToast(
"Connection timeout, please try again",
);
throw Exception();
} on SocketException catch (e) {
// print('Socket Error: $e');
showErrorToast(
"Please check your network connection",
);
throw Exception();
} on Error catch (e) {
// print('General Error: $e');
showErrorToast(
"Something went wrong",
);
throw Exception();
}
}