【问题标题】:Downloading a file from Mongoose in Angular在 Angular 中从 Mongoose 下载文件
【发布时间】:2016-04-09 01:13:42
【问题描述】:

我已经成功上传了一个文件并将其存储在我的 MongoDB 中,但现在我希望能够从同一个 mongoDB 下载该文件。在服务器端,我使用 Mongoose 中的 GridFS 模块通过 gfs-read/write-stream 上传和下载。 Mongoose 中的下载代码如下:

app.post('/Download', function (req, res) {
    grid.mongo = mongoose.mongo;
    var gfs = grid(conn.db);
    var readstream = gfs.createReadStream({
        filename: 'Program.cs'
    });
    readstream.pipe(res);
})

到目前为止,我有这个角度:

$scope.Download = function () {
        $http.post(url + "/Download")
        .success(function (res) {
            console.log(res);
        })       
 }

console.log 响应显示为here

我想将此内容保存到本地文件系统中的 .cs 文件中,还希望能够提示用户输入下载路径,我该怎么做?

【问题讨论】:

  • 你能在本地下载文件吗?
  • 这就是我想要完成的 :) 所以.. 还没有我只是在我的角度获取文件/文件内容并且不知道如何处理它以使其存储在本地文件

标签: angularjs node.js mongodb mongoose


【解决方案1】:

试试下面的代码

app.post('/Download', function (req, res) {

    var filenameId = "";// mention _id value of 'Program.cs'

    var filename = 'Program.cs';

    var _id = new ObjectID(filenameId );

    var gfs = new Grid(conn.db, "yourfile_collection_name");// default value is fs 

    gfs.get(_id, function(err, data) {    
        if (err)
          throw err;
        res.setHeader('Content-Disposition','attachment; filename="' + filename + '"');
        res.send(data);
      });
};

【讨论】:

  • 那么我在 Angular 中该怎么做?如何在本地存储文件?
  • 不返回“refferenceError: ObjectID is not defined”
  • 使用 gfs.get(filenameId , function(err, data) { 确保 filenameId 具有有效值
  • 现在返回:“ReferenceError: Grid is not defined”
  • 确保遵循 import var Grid = require('mongodb').Grid;
猜你喜欢
  • 1970-01-01
  • 2018-05-10
  • 1970-01-01
  • 1970-01-01
  • 2017-11-10
  • 2019-06-05
  • 2018-05-30
  • 2019-01-11
  • 2019-02-08
相关资源
最近更新 更多