从字符串在 node.js 中创建一个文本文件并将其流式传输作为响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21950049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:54:25  来源:igfitidea点击:

Create a text file in node.js from a string and stream it in response

node.jsexpressstream

提问by user1323136

  1. I am using express.js

  2. I have a string "Hello world!"

  3. I want a user to click on

    <a href=/download>Download</a>
    
  4. The user should get Hello.txt download with the text in it, NOT open a tab with the text.

  5. I have looked around for ways to achieve this, I am guessing it has something to do with creating readstreams from buffer and piping to response, but I most of the examples dealt with reading actual files from disk, I don't want to read from disk, i just want to respond with a file created from a string.

  1. 我正在使用 express.js

  2. 我有一个字符串“Hello world!”

  3. 我希望用户点击

    <a href=/download>Download</a>
    
  4. 用户应该下载带有文本的 Hello.txt,而不是打开带有文本的选项卡。

  5. 我已经四处寻找实现这一目标的方法,我猜这与从缓冲区和管道到响应创建读取流有关,但是我的大多数示例都涉及从磁盘读取实际文件,我不想从中读取磁盘,我只想用一个从字符串创建的文件来响应。

Thanks!

谢谢!

回答by tsturzl

I think I understand what you're trying to do. You want to send a .txt file to the client without actually creating a file on disc.

我想我明白你想要做什么。您想将 .txt 文件发送到客户端而不实际在光盘上创建文件。

This is actually pretty basic, and extremely easy. All you have to do is set your MIME type in the header, however most browsers don't download .txt files by default. They just open and display the contents.

这实际上是非常基本的,而且非常容易。您所要做的就是在标题中设置您的 MIME 类型,但是大多数浏览器默认情况下不会下载 .txt 文件。他们只是打开并显示内容。

var text={"hello.txt":"Hello World!","bye.txt":"Goodbye Cruel World!"};
app.get('/files/:name',function(req,res){
   res.set({"Content-Disposition":"attachment; filename=\"req.params.name\""});
   res.send(text[req.params.name]);
});

As a future note, you can send any data that's stored as a variable. If you have a buffer loaded with an image, for example, you can send that the same way by just changing the Content-Type, otherwise the browser has no idea what data you're sending, and express I believe sets the default type to text/html. Here is a good reference to Internet Media Types and MIME types.

作为未来的注意事项,您可以发送任何存储为变量的数据。如果您有加载图像的缓存,例如,您可以发送通过只是改变了同样的方式Content-Type,否则浏览器根本不知道您发送的数据,并表示相信集的默认类型text/html。这里是Internet Media Types 和 MIME types 的一个很好的参考。

回答by vijay kumar

this is working for me !

这对我有用!

var text="hello world";

res.setHeader('Content-type', "application/octet-stream");

res.setHeader('Content-disposition', 'attachment; filename=file.txt');

res.send(text);

回答by Dark Litss

Try this:

尝试这个:

router.get('/download', (req, res) => {
  var text = 'Hello world!'
  res.attachment('filename.txt')
  res.type('txt')
  res.send(text)
})

回答by user1323136

Thanks for the help guys, this is what i ended up with: @aaron, is there a way for disposition to work in all browsers?

感谢您的帮助,这就是我的最终结果:@aaron,有没有办法让配置在所有浏览器中工作?

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