解决方案下方是音频文件转换示例://文件转base64 readFileToBase64(path: string): string { // 打开文件 let srcFile = fs.openSync(path, fs.OpenMode.READ_ONLY); console.info(`file.size=${fs.statSync(srcFile.fd).size}`) let bufSize = 4096; let readSize = 0; let arrayBuf = new ArrayBuffer(bufSize); class Option { public offset: number = 0; public length: number = bufSize; } let option = new Option(); option.offset = readSize; let readLen = fs.readSync(srcFile.fd, arrayBuf, option); let buffs: buffer.Buffer[] = [] while (readLen > 0) { buffs.push(buffer.from(arrayBuf, 0, readLen)) readSize += readLen; option.offset = readSize; arrayBuf = new ArrayBuffer(bufSize); readLen = fs.readSync(srcFile.fd, arrayBuf, option); } let allBuff = buffer.concat(buffs) console.info(`readSize=${readSize} allbuff=${allBuff.buffer.byteLength}`) // 关闭文件 fs.closeSync(srcFile); return new util.Base64Helper().encodeToStringSync(new Uint8Array(allBuff.buffer)) }
解决方案
下方是音频文件转换示例: