Nodejs发送文件作为响应

时间:2012-04-06 16:06:29

标签: node.js

Expressjs框架有一个sendfile()方法。如果不使用整个框架,我该怎么做呢?我使用node-native-zip创建存档,我想将其发送给用户。

4 个答案:

答案 0 :(得分:150)

这是一个示例程序,它将通过从磁盘流式传输myfile.mp3(也就是说,它不会在发送文件之前将整个文件读入内存)。服务器侦听端口2000。

[更新] 正如@Aftershock在评论中提到的那样,util.pump已经消失,并被称为pipe的Stream原型上的方法取代;下面的代码反映了这一点。

var http = require('http'),
    fileSystem = require('fs'),
    path = require('path');

http.createServer(function(request, response) {
    var filePath = path.join(__dirname, 'myfile.mp3');
    var stat = fileSystem.statSync(filePath);

    response.writeHead(200, {
        'Content-Type': 'audio/mpeg',
        'Content-Length': stat.size
    });

    var readStream = fileSystem.createReadStream(filePath);
    // We replaced all the event handlers with a simple call to readStream.pipe()
    readStream.pipe(response);
})
.listen(2000);

取自http://elegantcode.com/2011/04/06/taking-baby-steps-with-node-js-pumping-data-between-streams/

答案 1 :(得分:9)

您需要使用Stream在响应中发送文件(存档),您还需要在响应标头中使用适当的Content-type。

有一个示例函数可以执行此操作:

const fs = require('fs');

// Where fileName is name of the file and response is Node.js Reponse. 
responseFile = (fileName, response) => {
  const filePath =  "/path/to/archive.rar" // or any file format

  // Check if file specified by the filePath exists 
  fs.exists(filePath, function(exists){
      if (exists) {     
        // Content-type is very interesting part that guarantee that
        // Web browser will handle response in an appropriate manner.
        response.writeHead(200, {
          "Content-Type": "application/octet-stream",
          "Content-Disposition": "attachment; filename=" + fileName
        });
        fs.createReadStream(filePath).pipe(response);
      } else {
        response.writeHead(400, {"Content-Type": "text/plain"});
        response.end("ERROR File does not exist");
      }
    });
  }
}
  

Content-Type字段的目的是充分描述正文中包含的数据,以便接收用户代理可以选择适当的代理或机制来向用户呈现数据,或以其他方式处理数据。适当的方式。

“application / octet-stream”在RFC 2046中定义为“任意二进制数据”,此内容类型的目的是保存到磁盘 - 这是您真正需要的。

“filename = [文件名]”指定将下载的文件名。

有关详细信息,请参阅this stackoverflow topic

答案 2 :(得分:0)

有点晚了,但 express 有一个帮手,可以让生活更轻松。

app.get('/download', function(req, res){
  const file = `${__dirname}/path/to/folder/myfile.mp3`;
  res.download(file); // Set disposition and send it.
});

答案 3 :(得分:0)

这对我有帮助。只要您点击 /your-route 路线,它就会开始下载文件。

app.get("/your-route", (req, res) => {

         let filePath = path.join(__dirname, "youe-file.whatever");

         res.download(filePath);
}

是的,download 也是一种快速方法。

相关问题