NodeJS 将string作为文件下载

我想要一个string并将其作为文件下载,而不保存到文件系统。

这是我的。

res.attachment('hello.text'); res.setHeader('Content-type', 'text/plain'); res.end("Hello, World", 'utf8'); 

我也试过这个:

 res.setHeader('Content-disposition', 'attachment; filename=theDocument.txt'); res.setHeader('Content-type', 'text/plain'); res.charset = 'UTF-8'; res.download("Hello, world"); res.end(); 

所有这些只是在ajax响应对象中返回Hello World。 我希望它强制下载。

你只需要使用:

 res.download('.tmp/public/img/image.jpg'); 

传递给res.download的值是要发送到客户端的文件的path。