简体   繁体   English

我的node.js流代码有什么问题?

[英]what's wrong with my node.js stream code?

I have a m4a audio file in my server, and I want to get it on my device. 我的服务器中有一个m4a音频文件,我想在设备上获取它。 I can get a file with this code, but I cannot play it on my phone and PC. 我可以获得带有此代码的文件,但无法在手机和PC上播放它。

I think that the file was transferred incorrectly. 我认为该文件传输不正确。 Can you give me some advice? 你能给我一些建议吗? (there are no error or warning messages on my server) (我的服务器上没有错误或警告消息)

app.get('/upload/:file',function(req,res){
  console.log('download');
  var filename = req.params.file;
  var path = './disk/'+filename;

  var stat = fs.statSync(path);

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

  var readStream = fs.createReadStream(path);
  readStream.setEncoding('utf8');
  readStream.pipe(res);
  console.log('done');
})

Remove this line: 删除此行:

readStream.setEncoding('utf8');

and the binary data will pass through un-modified (with this line it's currently being interpreted as utf-8 textual data). 并且二进制数据将未经修改地传递(此行当前被解释为utf-8文本数据)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM