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
165 views
in Technique[技术] by (71.8m points)

javascript - 使用Express从NodeJS Server下载文件(Download a file from NodeJS Server using Express)

How can I download a file that is in my server to my machine accessing a page in a nodeJS server?

(如何将服务器中的文件下载到访问nodeJS服务器中页面的计算机上?)

I'm using the ExpressJS and I've been trying this:

(我正在使用ExpressJS,并且一直在尝试这样做:)

app.get('/download', function(req, res){

  var file = fs.readFileSync(__dirname + '/upload-folder/dramaticpenguin.MOV', 'binary');

  res.setHeader('Content-Length', file.length);
  res.write(file, 'binary');
  res.end();
});

But I can't get the file name and the file type ( or extension ).

(但是我无法获取文件名和文件类型(或扩展名)。)

Can anyone help me with that?

(有人可以帮我吗?)

  ask by Thiago Miranda de Oliveira translate from so

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

1 Reply

0 votes
by (71.8m points)

Update(更新资料)

Express has a helper for this to make life easier.

(Express为此提供了一个帮助 ,使生活更轻松。)

app.get('/download', function(req, res){
  const file = `${__dirname}/upload-folder/dramaticpenguin.MOV`;
  res.download(file); // Set disposition and send it.
});

Old Answer(旧答案)

As far as your browser is concerned, the file's name is just 'download', so you need to give it more info by using another HTTP header.

(就您的浏览器而言,该文件的名称仅为“下载”,因此您需要使用另一个HTTP标头为其提供更多信息。)

res.setHeader('Content-disposition', 'attachment; filename=dramaticpenguin.MOV');

You may also want to send a mime-type such as this:

(您可能还希望发送如下的mime类型:)

res.setHeader('Content-type', 'video/quicktime');

If you want something more in-depth, here ya go.

(如果您想要更深入的了解,请继续。)

var path = require('path');
var mime = require('mime');
var fs = require('fs');

app.get('/download', function(req, res){

  var file = __dirname + '/upload-folder/dramaticpenguin.MOV';

  var filename = path.basename(file);
  var mimetype = mime.lookup(file);

  res.setHeader('Content-disposition', 'attachment; filename=' + filename);
  res.setHeader('Content-type', mimetype);

  var filestream = fs.createReadStream(file);
  filestream.pipe(res);
});

You can set the header value to whatever you like.

(您可以将标题值设置为任意值。)

In this case, I am using a mime-type library - node-mime , to check what the mime-type of the file is.

(在这种情况下,我使用的是mime类型的库-node-mime ,以检查文件的mime类型。)

Another important thing to note here is that I have changed your code to use a readStream.

(这里要注意的另一件事是,我已将您的代码更改为使用readStream。)

This is a much better way to do things because using any method with 'Sync' in the name is frowned upon because node is meant to be asynchronous.

(这是一种更好的处理方式,因为名称中的任何带有“ Sync”的方法都被禁止使用,因为node是异步的。)


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

...