簡體   English   中英

使用AngularJS下載多個文件並存儲在路徑中

[英]Downloading multiple files using AngularJS and storing in path

我是Angular的新手,我有一個場景,我需要同時下載多個文件。 我的文件存儲在GridFS中。 我可以下載文件,但是例如pdf為空白。 存儲在gridFS中的contentType是“ contentType”:“ binary / octet-stream”,我錯過了什么嗎?

我的翡翠代碼是

 tr(ng-repeat='row in displayedCollection')
                        td {{ row.Name}}
                        td {{ row.email}}
                        td
                            button.btn.btn-info(type='button',ng-click="downloadDocuments(row.documentsSubmitted)" download) Download Documents

我的控制器代碼是

   $scope.downloadDocuments = function (row) {
    angular.forEach(row, function (value, key) {
        var fileToDownload = value.id + "," + 'TrainingPartnerAddingTrainingCenter';

        $http.get('/downloadDocuments/' + fileToDownload).success(function (result, status, headers, config) {
            var _contentType = (headers('Content-Type'));
            var _fileName = headers('FileName');
            var blob = new Blob([ result ], { type : _contentType });
            var url = (window.URL || window.webkitURL).createObjectURL(blob);
            var anchor = angular.element('<a/>');
            anchor.attr({
                href : url,
                target : '_blank',
                download : _fileName
            })[0].click();
        });
    });
};

我的node.js代碼如下

exports.downloadDocument = function (req, res) {
var paramData = req.params.fileToDownload.split(',');
var role = req.session.user.role;
var conn = mongoose.connection;
var gfs = Grid(conn.db, mongoose.mongo);
routesLogger.logInfo(role, "downloadDocument", "START");
gfs.findOne({_id: paramData[0], root: paramData[1]}, function (err, file) {
    if (err) {
        routesLogger.logError(role, "downloadDocument", err);
        return res.status(400).send(err);
    }
    else if (!file) {
        routesLogger.logError(role, "downloadDocument", "No File Found for download");
        return res.status(404).send('Error on the database looking for the file.');
    }
    else {
        res.set('Content-Type', file.contentType);
        res.set('Content-Disposition', 'attachment; filename="' + file.filename + '"');

        var readstream = gfs.createReadStream({
            _id: paramData[0],
            root: paramData[1]
        });

        readstream.on("error", function (err) {
            routesLogger.logError(role, "downloadDocument", err);
            res.end();
        });
        readstream.pipe(res);
        routesLogger.logInfo(role, "downloadDocument", "END");
    }
});

};

所以我做的錯誤是沒有添加參數{responseType:'arraybuffer'}。 我在此鏈接中找到了答案

AngularJS:在角度應用程序中顯示blob(.pdf)

 $http.get('/downloadDocuments/' + fileToDownload,{ responseType: 'arraybuffer' }).success(function (result, status, headers, config) {
            console.log(headers('Content-Type'));
            var _contentType = (headers('Content-Type'));
            var _fileName = headers('FileName');
            var blob = new Blob([result], {type: _contentType });
            saveAs(blob, _fileName);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM