下载文件ERR

编程入门 行业动态 更新时间:2024-10-03 17:07:26

下载<a href=https://www.elefans.com/category/jswz/34/1771438.html style=文件ERR"/>

下载文件ERR

我需要下载我用 multer 上传的文件,并将其放入存储 Express 侧的文件夹名称 /uploads 的根目录中,然后将路径插入数据库中,我从数据库中获取路径

问题是,当我下载文件时,当我点击它时,文件会下载,这就是我得到的

无法访问您的文件

它可能已被移动、修改或删除。

ERR_FILE_NOT_FOUND

这是前端的代码

 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>
    ));

这是后端,我也是这样做的

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' });
  }
});
回答如下:

更多推荐

下载文件ERR

本文发布于:2024-05-31 06:48:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1771385.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   ERR

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!