5

我正在 Node.js 中构建一个文本编辑器,用户在其中在 textarea 中创建一个文件。完成文件编辑后,他可以按下“导出”按钮,触发 Jquery 函数,该函数读取文本区域并将文本发布到 node.js 服务器上。服务器应该读取信息并返回一个文件。我想避免在服务器上创建文件并为其提供服务,我宁愿使用流动态创建文件。我尝试使用以下方法,但没有奏效:

exports.exportfile = function(req,res){

  var Stream = require('stream')
  var stream = new Stream();

  res.setHeader('Content-disposition', 'attachment; filename=try.txt');
  res.setHeader('Content-type', 'text/plain');

  stream.pipe = function(dest) {
    dest.write('Hello Dolly')
  }

    stream.pipe(res)
}

有人对如何实现这一目标有任何见解吗?

4

1 回答 1

6

我在我的服务器上对此进行了测试,它工作正常。它来自 GET 调用,但我不明白为什么它不能与 POST 一起使用。

res.setHeader('Content-disposition', 'attachment; filename=theDocument.txt');
res.setHeader('Content-type', 'text/plain');
res.charset = 'UTF-8';
res.write("Hello, world");
res.end();
于 2013-08-27T16:43:31.907 回答