0

I need to download files that i have uploaded with multer and make it in the storage express side in the root in the folder name /uploads and i insert the path in the database i get the path from the database

the problem that when i download the file the file download when i click on it this is what i get

Unable to access your file

It may have been moved, modified or deleted.

ERR_FILE_NOT_FOUND

this is the code in frontend side

 tableCondition = currentPosts2.map((p, key) => (
      <tr key={key}>
        <td>{key + 1}</td>
        <td>{p.id_bon}</td>
        <td>{p.nom_fournisseur}</td>
        <td>{p.acheteur}</td>
        <td>{p.livreur}</td>
        <td>{p.type_bon}</td>
        <td><a href={`/download/${p.scanne_bon}`} download>Download Uploaded File</a></td>
        <td>{p.recepteur}</td>
        <td>{dateNow(p.datee)}</td>
        <td>{p.heure}</td>
      </tr>
    ));

this is the back end side and also i did this

app.use('/download', express.static('uploads'));
router.get('/download/:fileName', (req, res) => {
  const fileName = req.params.fileName;
  const filePath = path.join(__dirname, '../uploads', fileName);

  // Check if the file exists
  if (fs.existsSync(filePath)) {
    // Set the appropriate headers for the download
    res.setHeader('Content-Disposition', `attachment; filename="${fileName}"`);
    res.setHeader('Content-Type', 'application/octet-stream');

    // Send the file as a response
    res.sendFile(filePath);
  } else {
    res.status(404).json({ error: 'File not found' });
  }
});

0 Answers0