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

时间:2022-08-16 19:39:01

I have a m4a audio file in my server, and I want to get it on my device. I can get a file with this code, but I cannot play it on my phone and PC.

我的服务器上有一个m4a音频文件,我想在我的设备上获取它。我可以使用此代码获取文件,但我无法在手机和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');
})

1 个解决方案

#1


3  

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文本数据)。

#1


3  

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文本数据)。