【问题标题】:File not downloading express.js res.download文件未下载 express.js res.download
【发布时间】:2016-03-08 03:58:46
【问题描述】:

我使用json2xlsx npm 模块生成/导出了一个xlsx 文件,并下载了我正在使用res.download(file)express.js 功能的该文件。

参考:Download a file from NodeJS Server using Express

以下是我的代码:

var fs = require("fs");
var json2xls = require('json2xls');

app.use(json2xls.middleware);

app.get('/export/:id', function (req, res) {
    var id = req.params.id;
    db.collection('provider').findOne({_id: mongojs.ObjectId(id)}, function (err, doc) {
        var jsonArr = {};
        var arr = jsonArr = doc;
        var xls = json2xls(arr);
        fs.writeFileSync('data.xlsx', xls, 'binary'); //file exported

        //Now I want to download that file
        res.setHeader('Content-disposition', 'attachment; filename=data.xlsx');
        res.setHeader('Content-type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        res.download(__dirname + '/public/data.xlsx', function (error) {
          console.log(error);
        });

        //res.json(doc);
    });
});

查看html

        <div class="panel-body">

            <div class="form-group" ng-repeat="(key,value) in providerList"  ng-if="!$first">                    
                    <label>{{key.replace("_", " ") | uppercase}}</label>
                    <input type="text" class="form-control" id="title" placeholder="Enter Title" ng-model="providerList[key]">                    
            </div>

            <div class="well well-lg text-center bg-gray">                                        
                <button class="btn-lg btn-success" ng-click="export(id)">Export to Spreadsheet</button>
            </div>
        </div>

AngularJS 控制器代码:

$scope.export = function (id) {
            $http.put('/export/' + id, $scope.providerList).success(function (response) {
                 if (response) {
                    alert("File is successfully exported at: "+ response);
                }
            });
        };

错误:{ [Error: Request aborted] code: 'ECONNABORTED' }

更新错误

任何帮助将不胜感激。

【问题讨论】:

  • 必要的调试信息。 “不下载”是什么意思?有什么错误吗?此调用将在控制台中显示什么:res.download(__dirname + '/data.xlsx', function(error) { console.log(error)} );
  • 这是错误:{ [Error: Request aborted] code: 'ECONNABORTED' }
  • @stdob--: 更新后请检查
  • 如果您在发送前尝试使用临时随机名称而不是data.xlsx 保存文件怎么办?什么节目curl -I http://you.site/export/id

标签: javascript node.js excel express mean-stack


【解决方案1】:

res.download()之前设置响应头,

res.setHeader('Content-disposition', 'attachment; filename=data.xlsx');
res.setHeader('Content-type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
res.download(__dirname + '/data.xlsx');

更新:

根据json2xls, 你可以使用res.xls('data.xlsx', jsonArr);

为此,您必须为 json2xls 设置中间件

app.use(json2xls.middleware);

更新 2:

前端(AngularJS),参考this

【讨论】:

  • data.xlsx 正在生成?
  • 更新后请检查
  • @Mark :我已经更新了我的答案并且它正在工作,使用中间件。
  • 使用 res.xls('data.xlsx', jsonArr);而不是 res.download();直接将 json 数组传入 res.xls 函数。
  • 它现在可以工作,但它的响应却像上面一样显示在console &amp; alert上(检查更新的帖子)
【解决方案2】:

可能是您保存在根文件夹中的 .xlsx 文件。因此,将 .xlsx 文件移动到 public 文件夹下,并像下面这样以 express 启用它。

        app.use(express.static('./public'));

【讨论】:

  • 移至public并使用app.use(express.static(__dirname + "/public")); // express.static means we are telling the server to look for static file i.e. html,css,js etc. but still not working
  • 这里是代码:res.download(__dirname + '/public/data.xlsx');
  • 它没有显示任何错误但下载也无法正常工作
  • 更新后请检查
【解决方案3】:

我注意到您的 Angular 控制器生成了一个 $http.put,但您的后端期待一个 GET (app.get)。您确定这是您打算如何配置的吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-29
    • 2020-04-12
    • 1970-01-01
    • 2016-03-07
    • 1970-01-01
    • 2019-05-25
    • 1970-01-01
    • 2019-03-16
    相关资源
    最近更新 更多