我正在开发将 pdf 转换为音频的 webapp,所以每次当用户点击 pdf 中的任何一点时,我们都会从该索引(用户点击的)发送 4 行到 node.js 服务器,在那里它创建一个音频文件那些4行并发送回客户端。我很高兴从服务器获取二进制数据作为响应,然后我将该数据存储在一个 blob 中并使用 URL.createObjectURL 函数为其创建一个 url,但是当我尝试下载与该 URL 关联的 mpeg 文件时,它给出了一个错误,它说文件无法播放“此文件无法播放。这可能是因为文件类型不支持,文件扩展名不正确,或者文件损坏。” *我尝试了相同的 .txt 文件,我可以在记事本中查看结果,当我尝试将 blob 的内容类型更改为 application/octet-stream 时,它会给出二进制结果。
请帮我解决一下这个。或者有什么方法可以实现上述任务。我在检查网站自然阅读器网络时看到了这个想法,它使用与上面解释的相同的东西*
你好世界.vue
//Template
<a :href="url" download>download</a>
//
//script
async playAudio(index=0,text){
console.log("this is play audio",index);
this.indexval(index);
console.log("text",text)
const obj={index:index,display:this.display_contents}
axios.post('http://localhost:4000/upload/playaudio',obj)
.then(async(res)=>{
//creating a blob
const blob1=new Blob([res.data],{'type':'audio/mpeg'})
console.log("THis is the response of the data",res.data)
this.url=URL.createObjectURL(blob1);
})
.catch(err=>{
console.log("this is the error from audio",err)
})
},
索引.js
router.post('/',(req,res)=>{
console.log("this is the index of the clicked event ", req.body.index);
const pythonProcess = spawn('C://Users//hp//AppData//Local//Programs//Python//Python39//python.exe',["dummy2.py",req.body.display]);
pythonProcess.stdout.on('data', function (data){
const audioPath = "./Audio/test.mp3";
const audioSize = fs.statSync("./Audio/test.mp3").size;
console.log(audioSize,"size")
// const range = req.headers.range;
const CHUNK_SIZE = audioSize; // 1MB
const start = 0
const end = Math.min(start + CHUNK_SIZE, audioSize - 1);
// Create headers
const contentLength = end - start + 1;
console.log("this is the content length",contentLength)
const headers = {
"Content-Range": `bytes ${start}-${end}/${audioSize}`,
"Accept-Ranges": "bytes",
"Content-Length": contentLength,
"Content-Type": "audio/mpeg"
};
// HTTP Status 206 for Partial Content
res.writeHead(206, headers);
// create video read stream for this particular chunk
const audioStream = fs.createReadStream(audioPath, { start, CHUNK_SIZE });
// Stream the chunk to the client
audioStream.pipe(res);
})