UI_PASSPORT_DOC_DOWNLOAD

This commit is contained in:
venbaittech 2025-10-08 15:06:06 +05:30
parent 030d1487a6
commit bd7d538481
2 changed files with 118 additions and 17 deletions

View File

@ -2155,7 +2155,7 @@ class TravellerDetailsState extends State<TravellerDetails> {
children: [
GestureDetector(
onTap: () {
print('DOWNLOAD - $passportFile');
print('DOWNLOAD - $passportFile - ${widget.userIdApi} ');
if (passportFile != null) {
try {
@ -2204,23 +2204,27 @@ class TravellerDetailsState extends State<TravellerDetails> {
}
} else if (passportFileUrlFromApi != null) {
print("Raw file path: $passportFileUrlFromApi");
apiService.getPassportDocDownload(
context,
widget.userIdApi,
);
// Extract public part of the path starting from "/assets"
String cleanedPath = passportFileUrlFromApi!;
final index = passportFileUrlFromApi!.indexOf("/assets");
if (index != -1) {
cleanedPath = passportFileUrlFromApi!.substring(index);
}
final fullUrl =
'https://apitest.tripapprovaltool.com$cleanedPath';
print("Final download URL: $fullUrl");
final anchor =
html.AnchorElement(href: fullUrl)
..target = '_blank'
..download = selectedFileNames ?? "document.pdf"
..click();
// // Extract public part of the path starting from "/assets"
// String cleanedPath = passportFileUrlFromApi!;
// final index = passportFileUrlFromApi!.indexOf("/assets");
// if (index != -1) {
// cleanedPath = passportFileUrlFromApi!.substring(index);
// }
//
// final fullUrl =
// 'https://apitest.tripapprovaltool.com$cleanedPath';
// print("Final download URL: $fullUrl");
//
// final anchor =
// html.AnchorElement(href: fullUrl)
// ..target = '_blank'
// ..download = selectedFileNames ?? "document.pdf"
// ..click();
} else {
print("No file available to download.");
}

View File

@ -1145,6 +1145,103 @@ class ApiService {
}
}
Future<void> getPassportDocDownload(BuildContext context, userId) async {
// final String apiUrldata = '$apiUrl/api/plans/download?plan_id=$planId';
final String apiUrldata = '$apiUrl/api/downloadPassport?user_id=$userId';
// final String apiUrldata = '$apiUrl/auth/googlelogin';
final token = await getToken();
if (token == null) {
throw Exception('Token not found. Please log in.');
}
final response = await http.get(
Uri.parse(apiUrldata),
headers: {
'Authorization': 'Bearer $token',
'Content-Type': 'application/json',
'app-signature': 'ts-traveltool-2025-signature-123456',
},
);
if (response.statusCode == 200) {
try {
print("PDf Dowloaded");
final contentType = response.headers['content-type'] ?? '';
// print('contentType - $contentType');
// String fileName = "Document";
// if (contentType.contains("pdf")) {
// fileName += ".pdf";
// } else if (contentType.contains("png")) {
// fileName += ".png";
// } else if (contentType.contains("jpeg") ||
// contentType.contains("jpg")) {
// fileName += ".jpg";
// } else {
// // fallback (unknown type, save as bin)
// fileName += ".$contentType";
// }
final disposition = response.headers['content-disposition'] ?? '';
String fileName = "passport_$userId";
final regExp = RegExp(
r'filename[^;=\n]*=((['
'"]).*?\2|[^;\n]*)',
);
final match = regExp.firstMatch(disposition);
if (match != null) {
fileName = match.group(1)!.replaceAll('"', '');
}
// Create a blob from the response body
final blob = html.Blob([response.bodyBytes]);
// Generate a download URL for the blob
final url = html.Url.createObjectUrlFromBlob(blob);
// Create a link element to trigger the download
final anchor =
html.AnchorElement(href: url)
..setAttribute('download', fileName)
..click();
// Revoke the download URL to free up resources
html.Url.revokeObjectUrl(url);
} catch (e) {
throw Exception('Error parsing response: $e');
}
} else if (response.statusCode == 403) {
print("403-FORB");
await logout(context);
return null;
// throw Exception('Failed to load users');
} else if (response.statusCode == 404) {
// showDialog(
// context: context,
// builder: (BuildContext context) {
// return AlertDialog(
// title: Text('File not found.'),
// // content: Text('File not found.'),
// actions: [
// TextButton(
// child: Text('OK'),
// onPressed: () {
// Navigator.of(context).pop(); // Close the dialog
// },
// ),
// ],
// );
// },
// );
} else {
throw Exception('Failed to load plans');
}
}
Future<void> getForexPdfDownload(BuildContext context, forexId) async {
final String apiUrldata =
'$apiUrl/api/plans/forexDownload?forex_id=$forexId';