Spaces:
Running
Running
error:
Browse filesError loading file from local path. Please check the path and try again.
script.js
CHANGED
|
@@ -21,15 +21,34 @@ const csvPreview = document.getElementById('csvPreview');
|
|
| 21 |
if (localPathInput.value) {
|
| 22 |
try {
|
| 23 |
// Handle local path input
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
const blob = await response.blob();
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
} catch (error) {
|
| 28 |
-
|
| 29 |
-
|
| 30 |
return;
|
| 31 |
}
|
| 32 |
-
|
| 33 |
file = templateUpload.files[0];
|
| 34 |
} else {
|
| 35 |
alert('Please select a PDF file or enter a local path first');
|
|
@@ -39,12 +58,7 @@ const today = new Date();
|
|
| 39 |
const dateString = `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')}`;
|
| 40 |
|
| 41 |
try {
|
| 42 |
-
//
|
| 43 |
-
await window.showDirectoryPicker().then(async (dirHandle) => {
|
| 44 |
-
const todayDirHandle = await dirHandle.getDirectoryHandle(dateString, { create: true
|
| 45 |
-
});
|
| 46 |
-
|
| 47 |
-
// Initialize PDF.js
|
| 48 |
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.12.313/pdf.worker.min.js';
|
| 49 |
const fileHandle = await todayDirHandle.getFileHandle(file.name, { create: true });
|
| 50 |
const writable = await fileHandle.createWritable();
|
|
|
|
| 21 |
if (localPathInput.value) {
|
| 22 |
try {
|
| 23 |
// Handle local path input
|
| 24 |
+
if (!localPathInput.value.startsWith('http') && !localPathInput.value.startsWith('file://')) {
|
| 25 |
+
alert('Local paths must start with "file://" (e.g., file:///C:/path/to/file.pdf)');
|
| 26 |
+
return;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
const response = await fetch(localPathInput.value, {
|
| 30 |
+
mode: 'cors',
|
| 31 |
+
headers: {
|
| 32 |
+
'Content-Type': 'application/pdf'
|
| 33 |
+
}
|
| 34 |
+
});
|
| 35 |
+
|
| 36 |
+
if (!response.ok) {
|
| 37 |
+
throw new Error(`HTTP error! status: ${response.status}`);
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
const blob = await response.blob();
|
| 41 |
+
if (blob.size === 0) {
|
| 42 |
+
throw new Error('Empty file received');
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
file = new File([blob], localPathInput.value.split('/').pop() || 'document.pdf', { type: 'application/pdf' });
|
| 46 |
} catch (error) {
|
| 47 |
+
console.error('File loading error:', error);
|
| 48 |
+
alert(`Error loading file: ${error.message}\n\nMake sure:\n1. Path starts with file://\n2. File exists\n3. CORS is enabled for local files`);
|
| 49 |
return;
|
| 50 |
}
|
| 51 |
+
} else if (templateUpload.files.length) {
|
| 52 |
file = templateUpload.files[0];
|
| 53 |
} else {
|
| 54 |
alert('Please select a PDF file or enter a local path first');
|
|
|
|
| 58 |
const dateString = `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')}`;
|
| 59 |
|
| 60 |
try {
|
| 61 |
+
// Initialize PDF.js
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.12.313/pdf.worker.min.js';
|
| 63 |
const fileHandle = await todayDirHandle.getFileHandle(file.name, { create: true });
|
| 64 |
const writable = await fileHandle.createWritable();
|