39 lines
1.4 KiB
JavaScript
39 lines
1.4 KiB
JavaScript
// Function to download file from stream
|
|
window.downloadFileFromStream = async (fileName, contentStreamReference) => {
|
|
const arrayBuffer = await contentStreamReference.arrayBuffer();
|
|
const blob = new Blob([arrayBuffer]);
|
|
const url = URL.createObjectURL(blob);
|
|
const anchorElement = document.createElement('a');
|
|
anchorElement.href = url;
|
|
anchorElement.download = fileName ?? '';
|
|
anchorElement.click();
|
|
anchorElement.remove();
|
|
URL.revokeObjectURL(url);
|
|
};
|
|
|
|
// Function to download file directly from URL
|
|
window.downloadFileFromUrl = async (url, fileName) => {
|
|
try {
|
|
const response = await fetch(url);
|
|
if (!response.ok) {
|
|
const errorText = await response.text();
|
|
throw new Error(`HTTP error! status: ${response.status}, message: ${errorText}`);
|
|
}
|
|
const blob = await response.blob();
|
|
const blobUrl = URL.createObjectURL(blob);
|
|
const anchorElement = document.createElement('a');
|
|
anchorElement.href = blobUrl;
|
|
anchorElement.download = fileName ?? '';
|
|
document.body.appendChild(anchorElement);
|
|
anchorElement.click();
|
|
document.body.removeChild(anchorElement);
|
|
URL.revokeObjectURL(blobUrl);
|
|
} catch (error) {
|
|
console.error('Error downloading file:', error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
|
|
|