Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js Request Stream Pipe Rename Filename

I am using Node.js Request module to server file form remote server to user Browser .

Here is code i use :

expressjs_app.get("/file_url", function(req, res){
   request.get('remote_file_url').pipe(res);
});

Everything works fine , I just want know it possible to change filename for user in Browser ?

Update

I sending remote file to user for download , i want when user want save file there is be different name other than orginal file name.

For example if remote file is: http:// domain.com/file1.zip i want change filename to http:// mydomain.com/myfile.zip

like image 219
user1086010 Avatar asked Aug 21 '15 10:08

user1086010


People also ask

What is __ filename in Node?

The __filename represents the filename of the code being executed. This is the resolved absolute path of this code file. For a main program, this is not necessarily the same filename used in the command line. The value inside a module is the path to that module file.

What is fs createWriteStream?

The createWriteStream() method is an inbuilt application programming interface of fs module which allows to quickly make a writable stream for the purpose of writing data to a file. This method may be a smarter option compared to methods like fs. writeFile when it comes to very large amounts of data.

What is a Readstream?

A readable stream is an abstraction for a source from which data can be consumed. An example of that is the fs. createReadStream method. A writable stream is an abstraction for a destination to which data can be written.

What is stream PassThrough ()?

PassThrough. This Stream is a trivial implementation of a Transform stream that simply passes the input bytes across to the output.


1 Answers

You need to set a header in the response:

expressjs_app.get("/file_url", function(req, res){
   res.header('Content-Disposition', 'attachment; filename="new file name.pdf"');
   request.get('remote_file_url').pipe(res);
});

Easy enough. Good luck.

like image 97
smremde Avatar answered Oct 13 '22 12:10

smremde