Nodejs多种方法实现文件下载

Nodejs多种方法实现文件下载

exports.download = function(req,res){
var downloadPath = Config.outName;
console.log(‘downloadPath=’+downloadPath);
res.download(downloadPath);   //express自带下载
/path.exists(downloadPath, function(exists) {
console.log("exists: ", exists);
if (exists) {
var fileStream = fs.createReadStream(downloadPath);
res.writeHead(200, {“Content-Type”:“application/octet-stream”});
fileStream.pipe(res);
console.log(‘fileStream pipe’);
fileStream.on(“end”, function() {
res.end();
})
}
});/
/*var filename = path.basename(downloadPath);
var mimetype = mime.lookup(downloadPath);        //匹配文件格式

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

var filestream = fs.createReadStream(downloadPath);
filestream.on('data', function(chunk) {
	res.write(chunk);
});
filestream.on('end', function() {
	res.end();
});*/

};


5 回复

Node.js 多种方法实现文件下载

在 Node.js 中实现文件下载可以通过多种方法来完成。下面我们将介绍几种常见的方法,并附带示例代码。

方法一:使用 Express 的 res.download() 方法

res.download() 是 Express 提供的一个方便的方法,用于发送文件给客户端进行下载。该方法会自动处理 HTTP 响应头,设置正确的 MIME 类型,并且能够处理文件名的编码问题。

exports.download = function(req, res) {
  const downloadPath = Config.outName;
  console.log('downloadPath=' + downloadPath);
  
  // 使用 Express 自带的下载功能
  res.download(downloadPath);
}

方法二:手动创建读取流并写入响应

如果你需要更多的控制权,比如自定义 HTTP 响应头或者处理错误,可以使用 Node.js 的内置模块 fshttp 来手动创建一个读取流并将文件内容写入到 HTTP 响应中。

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

exports.download = function(req, res) {
  const downloadPath = Config.outName;
  console.log('downloadPath=' + downloadPath);

  const filename = path.basename(downloadPath);
  const mimetype = mime.lookup(downloadPath);

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

  const fileStream = fs.createReadStream(downloadPath);
  fileStream.on('data', function(chunk) {
    res.write(chunk);
  });

  fileStream.on('end', function() {
    res.end();
  });
}

总结

以上两种方法都可以实现文件下载,具体选择哪种方法取决于你的需求。如果希望简单快速地实现文件下载功能,推荐使用 res.download() 方法;如果需要更细粒度的控制,比如自定义响应头或处理错误,可以选择手动创建读取流的方式。

通过这些方法,你可以灵活地根据项目需求来实现文件下载功能。


代码乱就乱吧,可是感觉都不全啊,还是我太菜了。。。

垃圾东西,代码都不全。

为了实现文件下载功能,我们可以使用几种不同的方法。以下是三种常见的方法,并附上相应的示例代码。

方法1:使用Express内置函数 res.download()

这种方法是最简单直接的方式,利用了Express框架的内置功能来处理文件下载。

exports.download = function(req, res) {
    var downloadPath = Config.outName;
    console.log('downloadPath=' + downloadPath);
    res.download(downloadPath);
}

这种方式会自动设置响应头,包括文件名、内容类型等信息。

方法2:手动设置响应头并流式传输文件

这种方法不依赖于Express内置函数,而是通过手动设置响应头,然后创建一个文件读取流,将文件内容写入到响应中。

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

exports.download = function(req, res) {
    var downloadPath = Config.outName;
    console.log('downloadPath=' + downloadPath);

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

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

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

这里我们手动设置了Content-DispositionContent-Type头,并且通过创建一个文件读取流(fs.createReadStream()),将文件内容直接流式传输给客户端。

方法3:使用第三方库如express-fileupload

这是一个更高级的方法,利用专门用于文件上传/下载的第三方库来处理这些操作。

首先需要安装库:

npm install express-fileupload --save

然后可以像这样使用它来处理文件下载:

const fileUpload = require('express-fileupload');

app.use(fileUpload());

exports.download = function(req, res) {
    const filePath = Config.outName;
    res.download(filePath);
}

此方法与第一种方法类似,但提供了更多配置选项和灵活性。

以上是三种实现文件下载的方法,你可以根据具体需求选择合适的方法。

回到顶部