我正在尝试从客户端的响应数据下载音频文件。但是它会损坏音频文件,并且尺寸会变大。我的代码有什么问题吗?
服务器端代码
s3Client.get(key)
.on('response', function (s3res) {
res.setHeader('Content-Length', s3res.headers['content-length']);
res.setHeader('Content-Type', s3res.headers['content-type']);
res.setHeader("Content-Disposition", "attachment; filename=" + track.title + ".mp3");
if (req.fresh) {
res.statusCode = 304;
res.end();
return;
}
if (req.method === 'HEAD') {
res.statusCode = 200;
res.end();
return;
}
s3res.pipe(res);
}
).end();
//Client Side JavaScript
this.trackService.downloadTrack(track)
.then((result) =>{
//var convertedBuffer = new Uint8Array(result.data);
const url = window.URL.createObjectURL(new Blob([result.data],{type: 'audio/mpeg'}));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', track.title + '.mp3');
document.body.appendChild(link);
link.click();
}, (error) =>{
console.error(error);
})