【问题标题】:"Http failure during parsing" when trying to download excel file created in nodejs (exceljs) (FIXED)尝试下载在 nodejs (exceljs) 中创建的 excel 文件时出现“解析期间的 Http 失败”(已修复)
【发布时间】:2019-09-04 11:54:21
【问题描述】:

我正在尝试将在 nodejs 中创建的 xls 文件下载到客户端(使用 exceljs)。 创建基于http://www.ihamvic.com/2018/07/25/create-and-download-excel-file-in-node-js/

由于某种原因 - 我无法在客户端中保存文件 - 我在订阅 getExcel 可观察对象时收到“解析期间的 Http 失败”。 我错过了一些标题定义吗? 查看我的代码:

这是nodejs端:

const express = require('express');
const router = express.Router();
const excel = require('./excel');

router.use(req, res, next) =>
{
    //The getDataByPromise is a function that return a promise
    return getDataByPromise(req.body.request).then((dataResult) => {
        let reportName = req.body.reportName ? req.body.reportName : '';
        return excel.createExcel(res, dataResult, reportName);
    }).catch((err) => {
        next({
            details: err
        })
    });
})

module.exports = router;

这是带有createExcel功能的excel模块:

module.exports = 
{
    createExcel : function(res, dataResult, reportTypeName)
    {
        let workbook = new excel.Workbook();
        let worksheet = workbook.addWorksheet('sheet1');
        dataResult.forEach(dataItem => worksheet.addRow(dataItem)); //Insert data into the excel

      //SOLUTION - SOLVED
      res.setHeader('Content-Type', 'application/vnd.openxmlformats- 
      officedocument.spreadsheetml.sheet');
      res.setHeader("Content-Disposition", "attachment; 
      filename=votingboxes.xlsx");
      workbook.xlsx.write(res).then(() => 
     {
        res.end();
     })


}

这是在客户端接近 nodejs(我们称之为 srv)的服务:

getExcel(request : any , reportName : string ) : Observable<any>
{
    var path = <relevant path to the nodejs endpoint>;
    const options = { withCredentials: true };

    return this.http.post<any>(path, {request: request, reportName : reportName }, options) //This route to the getDataByPromise function
}

这是组件函数:

exportToExcel() : void
{
    this.srv.getExcel(votingBoxRequestForExcel, this.reportTypeNameForExcel).subscribe(result => 
    {
      const data: Blob = new Blob([result], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8'});

      //FileSaver is file-saver package
      FileSaver.saveAs(data, 'test.xlsx');
    }, 
    error =>  console.log(error) //Reaching to the error instead of the response
  );    
}

【问题讨论】:

    标签: node.js angular express filesaver.js exceljs


    【解决方案1】:

    你需要告诉 Angular 它可以期待什么类型的响应,所以你可以添加

    响应类型

    到您的 http 选项:

    getExcel(request : any , reportName : string ) : Observable<any>
    {
        var path = <relevant path to the nodejs endpoint>;
        const options = { withCredentials: true, responseType: 'blob' };
    
        return this.http.post<any>(path, {request: request, reportName : reportName }, options) //This route to the getDataByPromise function
    }
    

    【讨论】:

      【解决方案2】:

      找到了一个解决方案 - 我使用了错误的方法:我没有使用带有 promise 的 writeFile 然后 res.sendFile - 我将响应的标题设置为“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”内容类型和“内容处置”、“附件;文件名=votingboxes.xlsx”,然后 使用发送工作簿“写”方法的响应发送 - 查看更正的代码:

      res.setHeader('Content-Type', 'application/vnd.openxmlformats- 
      officedocument.spreadsheetml.sheet');
          res.setHeader("Content-Disposition", "attachment; filename=votingboxes.xlsx");
          workbook.xlsx.write(res).then(() => 
          {
              res.end();
          })
      

      我也会在代码中更正它

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-20
        • 2021-03-08
        • 1970-01-01
        • 2020-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-06
        相关资源
        最近更新 更多