Uvix commited on
Commit
a50a09a
·
verified ·
1 Parent(s): dbda29e

Error loading file from local path. Please check the path and try again.

Files changed (1) hide show
  1. script.js +25 -11
script.js CHANGED
@@ -21,15 +21,34 @@ const csvPreview = document.getElementById('csvPreview');
21
  if (localPathInput.value) {
22
  try {
23
  // Handle local path input
24
- const response = await fetch(localPathInput.value);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  const blob = await response.blob();
26
- file = new File([blob], localPathInput.value.split('\\').pop(), { type: 'application/pdf' });
 
 
 
 
27
  } catch (error) {
28
- alert('Error loading file from local path. Please check the path and try again.');
29
- console.error(error);
30
  return;
31
  }
32
- } else if (templateUpload.files.length) {
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
- // Create directory for today's date if it doesn't exist
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();