Add proxy endpoint to fix CORS issues for file previews
- Create new `/api/files/proxy-preview` endpoint that fetches and serves files directly - Update FilePreviewModal to use proxy endpoint for images and PDFs - Set appropriate headers for inline display and caching - Remove trailing slash from folder display names in file list
This commit is contained in:
@@ -155,13 +155,16 @@ const loadPreview = async () => {
|
||||
previewUrl.value = '';
|
||||
|
||||
try {
|
||||
const response = await $fetch('/api/files/preview', {
|
||||
params: { fileName: props.file.name },
|
||||
});
|
||||
|
||||
previewUrl.value = response.url;
|
||||
// For images and PDFs, use the proxy endpoint to avoid CORS issues
|
||||
if (isImage.value || isPdf.value) {
|
||||
// Use the proxy endpoint that serves the file directly
|
||||
previewUrl.value = `/api/files/proxy-preview?fileName=${encodeURIComponent(props.file.name)}`;
|
||||
// The loading state will be handled by the image/iframe onload event
|
||||
} else {
|
||||
throw new Error('File type does not support preview');
|
||||
}
|
||||
} catch (err: any) {
|
||||
error.value = err.data?.statusMessage || 'Failed to load preview';
|
||||
error.value = err.data?.statusMessage || err.message || 'Failed to load preview';
|
||||
loading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user