从下载一个猫鼬文件中的角 [英] Downloading a file from Mongoose in Angular

查看:151
本文介绍了从下载一个猫鼬文件中的角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功上传文件并将其保存在我的MongoDB的,但现在我希望能够从相同的MongoDB立即下载该文件。在服务器端,我用猫鼬的GridFS的模块,上传和使用GFS读/写流下载。
在猫鼬立即下载code如下:

I've managed to upload a file and store it in my MongoDB, but now I want to be able to downlaod this file from the same mongoDB. In server-side I'm using the GridFS module in Mongoose to upload and download using the gfs-read/write-stream. Downlaod code in Mongoose looks like :

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

在我的角度,我有这个至今:

In my angular i have this so far:

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

在的console.log响应如图这里

我想这个内容到一个cs文件保存在我的本地文件系统,也希望能够提示下载路径的用户,我该怎么做呢?

I want to save this content into a .cs file in my local file system, also want to be able to prompt the user for the download-path, How do I do this?

推荐答案

尝试下面code

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

这篇关于从下载一个猫鼬文件中的角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆