Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

filestream - Dynamically creating a file with node.js and make it available for download

I am building a text editor in Node.js where a user create a file in a textarea. When he is done editing the file, he can push an "export" button that triggers a Jquery function that reads the textarea and post the text on the node.js server. The server should read the information and return a file. I would like to avoid creating a file on the server and servicing it, I'd rather create a file on the fly with streams. I have tried using the following and but that didn't work:

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)
}

Does anybody have any insight on how to achieve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I tested this on my server and it worked. It was from a GET call but I don't see why it wouldn't work with a 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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...