Fix file downloads for Safari with proper filename handling
Implement browser-specific download methods to ensure files download with correct filenames across all browsers. Safari now uses window.location.href while other browsers use blob URLs. Add Content-Disposition header to proxy endpoint for proper filename preservation.
This commit is contained in:
@@ -199,33 +199,51 @@ const handlePreviewError = (event: any) => {
|
||||
loading.value = false;
|
||||
};
|
||||
|
||||
// Download file
|
||||
// Download file (with special handling for Safari)
|
||||
const downloadFile = async () => {
|
||||
if (!props.file) return;
|
||||
|
||||
try {
|
||||
// Use proxy download endpoint for better mobile compatibility
|
||||
const proxyUrl = `/api/files/proxy-download?fileName=${encodeURIComponent(props.file.name)}`;
|
||||
|
||||
// Create a link element
|
||||
const link = document.createElement('a');
|
||||
link.href = proxyUrl;
|
||||
|
||||
// Extract clean filename for download
|
||||
let filename = props.file.displayName;
|
||||
if (!filename.includes('.') && props.file.extension) {
|
||||
filename += '.' + props.file.extension;
|
||||
}
|
||||
|
||||
link.download = filename;
|
||||
link.style.display = 'none';
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
// Check if Safari (iOS or desktop)
|
||||
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
|
||||
|
||||
// Clean up
|
||||
setTimeout(() => {
|
||||
document.body.removeChild(link);
|
||||
}, 100);
|
||||
if (isSafari) {
|
||||
// For Safari, use location.href to force proper filename handling
|
||||
const downloadUrl = `/api/files/proxy-download?fileName=${encodeURIComponent(props.file.name)}`;
|
||||
window.location.href = downloadUrl;
|
||||
} else {
|
||||
// For other browsers, use blob approach
|
||||
const response = await fetch(`/api/files/proxy-download?fileName=${encodeURIComponent(props.file.name)}`);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to download file');
|
||||
}
|
||||
|
||||
const blob = await response.blob();
|
||||
|
||||
// Create object URL from blob
|
||||
const downloadUrl = URL.createObjectURL(blob);
|
||||
|
||||
// Create a link element
|
||||
const link = document.createElement('a');
|
||||
link.href = downloadUrl;
|
||||
link.download = filename;
|
||||
link.style.display = 'none';
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
|
||||
// Clean up
|
||||
setTimeout(() => {
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(downloadUrl);
|
||||
}, 100);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to download file:', err);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user