TypeError:标头内容包含无效字符

问题描述 投票:-1回答:1

以下是我从我的服务器中用sailsJS编写文件的功能。当我尝试运行此功能时,控制台会向我抛出此类错误:TypeError: The header content contains invalid characters

在邮递员中,我以pdf格式发送文件,

内容类型在这里看起来如何?

fn: async function ({ id }, exits) {

    this.res.set('Content-disposition', `attachment; filename=${id}`, 'content-type', 'application/pdf')//here
    this.res.set('Access-Control-Allow-Origin', '*')

    const localPath = path.join(sails.config.custom.uploadDirectory, id)

    this.res.sendFile(localPath);
  }

预先感谢

javascript node.js
1个回答
0
投票

根据SailJS Documentation here,您可以一次将单个对象参数(标题)传递给设置多个标题字段,其中keys是标题字段名称,而对应的values是所需的值。

我认为您没有以正确的方式设置标题值。

如果设置多个值,它应该发送完整的对象:

res.set({
  'Content-disposition': 'attachment',
  'content-type': 'application/pdf',
  'filename'= 'filename.pdf'
})
© www.soinside.com 2019 - 2024. All rights reserved.