Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Express.js:res.attachment和res.download有什么区别?_Node.js_Express - Fatal编程技术网

Node.js Express.js:res.attachment和res.download有什么区别?

Node.js Express.js:res.attachment和res.download有什么区别?,node.js,express,Node.js,Express,res.attachment和res.download之间有什么区别?什么时候使用我有点困惑。在我的例子中,我将表单数据作为JSON发送到服务器,服务器将从中创建一个文件,并通过下载链接进行响应。这两个选项中的哪一个:res.attachment和res.download我应该使用?res.attachment“将HTTP响应内容处置头字段设置为“attachment” 这本质上是说,‘嘿,这个文件应该被视为附件,而不是网页。’ 而res.download“将路径处的文件作为“附件”传输。通常,

res.attachment
res.download
之间有什么区别?什么时候使用我有点困惑。在我的例子中,我将表单数据作为JSON发送到服务器,服务器将从中创建一个文件,并通过下载链接进行响应。这两个选项中的哪一个:
res.attachment
res.download
我应该使用?

res.attachment“将HTTP响应内容处置头字段设置为“attachment”

这本质上是说,‘嘿,这个文件应该被视为附件,而不是网页。’

res.download
“将路径处的文件作为“附件”传输。通常,浏览器会提示用户下载。”


查找有关每个的详细信息

Ok。。因此,如果我执行res.attachment,我需要单独执行response.sendfile来实际发送文件。但是res.download.Do res.attachment(filename.zip)创建文件或写入流时不需要这样做?@NeerajSewani目前还不清楚两者之间的区别。在这两种情况下,响应都是发送回客户端的文件!我阅读了文档,除了res.download()提供了用于错误处理的回调函数外,我没有看到明显的区别。两者之间还有其他区别吗?两者之间有一些关键区别。正如x7R5fQ指出的,
.download
有一个用于错误处理的
回调
参数以及一个
选项
参数,如果需要,内容将传递到
.sendFile()
方法。对于
.attachment()
,此方法确实返回对象,而
.download()
没有返回值。