gpt4 book ai didi

javascript - 类型错误 : The header content contains invalid characters

转载 作者:行者123 更新时间:2023-11-30 23:57:13 24 4
gpt4 key购买 nike

下面是我用 sailsJS 编写的从服务器获取文件的函数。当我尝试运行此函数时,我的控制台向我抛出此类错误:类型错误: header 内容包含无效字符

在 postman 中,我以 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);
}

如何在浏览器中打开文件而不是将其下载到桌面?

提前致谢

最佳答案

根据 SailJS Documentation here ,您可以传入单个对象参数( header )来一次设置多个 header 字段,其中是 header 字段名称和相应的 是所需的值。

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

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

res.set({
'Content-disposition': 'attachment',
'content-type': 'application/pdf',
'filename': 'filename.pdf'
})

[编辑]:为了在浏览器中打开文件,您应该添加

Content-Disposition: `inline;filename="${id}"`

关于javascript - 类型错误 : The header content contains invalid characters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60971337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com