i have this front end i download files from a folder for each user the folder structure i see the files correctly in their subfolders but i can’t download it look only in the main folder when i try to download.
012345678
ΕΣΩΤΕΡΙΚΑ.pdf[ΕΣΩΤΕΡΙΚΑ.pdf]
Παραστατικά-3.jpg[Παραστατικά-3.jpg]
Παραστατικά-4.jpg[Παραστατικά-4.jpg]
Παραστατικά-5.jpg[Παραστατικά-5.jpg]
testfolder
Νέος φάκελος
327688-EFT POS (Α1155 & Α1098).pdf[327688-EFT POS (Α1155 & Α1098).pdf]
ΤΗΛΕΦΩΝΑ.txt[ΤΗΛΕΦΩΝΑ.txt]
async function fetchFolderStructure() {
try {
const response = await fetch(‘/get-folder-structure’);
if (response.ok) {
const folderStructure = await response.json();
renderFolderStructure(folderStructure, document.getElementById(‘file-tree’));
} else {
console.error(‘Failed to load folder structure:’, response.statusText);
}
} catch (error) {
console.error(‘Error fetching folder structure:’, error);
}
}
function renderFolderStructure(node, container) {
const element = document.createElement(‘div’);
element.classList.add(node.type);
element.textContent = (node.type === ‘folder’ ? ' ’ : ' ') + node.name;
if (node.type === 'folder') {
element.classList.add('folder');
element.addEventListener('click', () => {
const nextSibling = element.nextElementSibling;
if (nextSibling.classList.contains('hidden')) {
nextSibling.classList.remove('hidden');
} else {
nextSibling.classList.add('hidden');
}
});
} else if (node.type === 'file') {
element.classList.add('file');
const link = document.createElement('a');
link.href = `/download/${encodeURIComponent(node.name)}`;
link.textContent = node.name;
element.appendChild(link);
}
container.appendChild(element);
if (node.children) {
const childrenContainer = document.createElement('div');
childrenContainer.classList.add('hidden');
container.appendChild(childrenContainer);
// First, render files
node.children
.filter(child => child.type === 'file')
.forEach(child => renderFolderStructure(child, childrenContainer));
// Then, render folders
node.children
.filter(child => child.type === 'folder')
.forEach(child => renderFolderStructure(child, childrenContainer));
}
}
document.addEventListener(‘DOMContentLoaded’, fetchFolderStructure);
server.js
app.get(‘/download/*’, (req, res) => {
if (!req.session.vat) {
return res.status(401).json({ error: ‘Unauthorized’ });
}
// Capture everything after '/download/'
const vat = req.session.vat.toString();
const relativeFilePath = decodeURIComponent(req.params[0]); // Get the relative file path from URL
const filePath = path.join('D:', vat, relativeFilePath); // Construct the full file path
console.log('Attempting to download file at:', filePath);
// Check if the file exists
fs.access(filePath, fs.constants.F_OK, err => {
if (err) {
console.error('File not found or access error:', err);
return res.status(404).json({ error: 'File not found' });
}
// Send the file
res.download(filePath, err => {
if (err) {
console.error('Error downloading file:', err);
res.status(500).json({ error: 'Error downloading file' });
}
});
});
});