Mongodb 和 GridFS 文件下载

huangapple go评论83阅读模式
英文:

Mongodb and Gridfs file download

问题

我正在构建一个允许用户上传和下载文件的应用程序。目前我遇到的问题是,我可以从我的MongoDB数据库检索文件并通过我的机器将其下载到本地,但无法使其通过浏览器下载。

到目前为止,我尝试过这样做:

const conn = mongoose.createConnection(process.env.URL, {
    useNewUrlParser: true,
    useUnifiedTopology: true
})

const bucket = new GridFSBucket(conn, {bucketName: 'uploads'});

router.get('/download', (req, res) => {

    const files = bucket.openDownloadStreamByName('3987a162969935378c4f8cbe5d95bb46.svg').pipe(fs.createWriteStream('./3987a162969935378c4f8cbe5d95bb46.svg'))
    res.end()
})
英文:

I'm building an application that allows the user to upload and download files. The issue i'm currently having is that I can retrieve the file from my MongoDB database and download it locally through my machine, but I can't get it to download through the browser.

So far I have tried doing it like this

const conn = mongoose.createConnection(process.env.URL, {
    useNewUrlParser: true,
    useUnifiedTopology: true
})

const bucket = new GridFSBucket(conn, {bucketName: 'uploads'});


router.get('/download', (req, res) => {

    const files = bucket.openDownloadStreamByName('3987a162969935378c4f8cbe5d95bb46.svg').pipe(fs.createWriteStream('./3987a162969935378c4f8cbe5d95bb46.svg'))
    res.end()
})

答案1

得分: 1

如果我们想要直接将文件发送给客户端而不写入磁盘,请使用以下方法:

后端

router.get('/download', (req, res) => {
  res.set('Content-disposition', 'attachment; filename="your_file_name.svg"');
  res.set('Content-Type', 'image/svg+xml');
  bucket.openDownloadStreamByName('your_file_name.svg').pipe(res);
})

前端

fetch('http://localhost:8000/download')
      .then(res => res.blob())
      .then(data => {
        saveAs(data, 'your_file_name.svg');
      })
英文:

If we want to send file directly to client without writing to disk, use this:

backend

router.get('/download', (req, res) => {
  res.set('Content-disposition', `attachment; filename="your_file_name.svg"`);
  res.set('Content-Type', 'image/svg+xml');
  bucket.openDownloadStreamByName('your_file_name.svg').pipe(res);
})

frontend

fetch('http://localhost:8000/download')
      .then(res => res.blob())
      .then(data => {
        saveAs(data, 'your_file_name.svg');
      })

答案2

得分: 0

I do not think the issue is with mongoDB or Gridfs its how you are dealing with the file stream. If you want the file to be downloaded once the user calls /GET /download you need to use - res.download(file); e.g.

不是mongoDB或Gridfs的问题,而是你如何处理文件流的问题。如果你希望用户在调用/GET /download时下载文件,你需要使用 - res.download(file); 例如。

Good Luck!
祝你好运!
也访问这个 https://stackoverflow.com/questions/7288814/download-a-file-from-nodejs-server-using-express

英文:

I do not think the issue is with mongoDB or Gridfs its how you are dealing with the file stream. If you want the file to be downloaded once the user calls /GET /download you need to
use - res.download(file); e.g.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const conn = mongoose.createConnection(process.env.URL, {
  useNewUrlParser: true,
  useUnifiedTopology: true
})

const bucket = new GridFSBucket(conn, {
  bucketName: &#39;uploads&#39;
});


router.get(&#39;/download&#39;, (req, res) =&gt; {

  const files = bucket.openDownloadStreamByName(&#39;3987a162969935378c4f8cbe5d95bb46.svg&#39;).pipe(fs.createWriteStream(&#39;./3987a162969935378c4f8cbe5d95bb46.svg&#39;))
  res.download(files)
})

<!-- end snippet -->

Good Luck!
Also visit this https://stackoverflow.com/questions/7288814/download-a-file-from-nodejs-server-using-express

huangapple
  • 本文由 发表于 2023年1月6日 07:43:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75025548.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定