错误为:“错误[ERR_STREAM_WRITE_AFTER_END]:结束后写入”
好吧,似乎我对此主题缺乏理解,因为我确实在stackoverflow中搜索了此错误,并且有很多有关此的信息。
仍然,我无法解决我的问题。
问题是:它第一次起作用。第二次给出错误。
所以我正在尝试将HTML页面转换为docx,一切都很好。
docx不会写入文件夹,而是写入Blob。
然后,我尝试从该Blob创建流并将其通过管道传输到Azure Blob存储。
我认为我在这里缺少什么。
代码:
import * as HtmlDocx from "html-docx-js";
import * as fs from "fs";
import * as path from "path";
import * as stream from "stream";
import { AzureStorageService } from "../services/azure-storage.service";
const bufferStream: any = new stream.PassThrough();
export class DOCXGenerator {
static async generateDocx(_data: any): Promise<any> {
const inputFile: string = `${path.join(
__dirname,
"../templates/index.html"
)}`;
const outputFile: string = `${path.join(
__dirname,
"../uploads/" + _data.subtitle
)}`;
await fs.readFile(inputFile, "utf-8", async function(err, html) {
let newHTML: any = html.replace(
"uri",
_data.subtitleURL.replace("/", "")
);
let newNewHTML: any = newHTML.replace(
"titlesub",
_data.subtitle.replace("/", "")
);
if (err) {
throw err;
}
// ERROR STARTS HERE:
const docxAsBlob: any = await HtmlDocx.asBlob(newNewHTML);
bufferStream.write(new Buffer(docxAsBlob));
bufferStream.pipe(
await AzureStorageService.createWriteStreamToBlockBlob(
"container-name",_data.subtitle
)
);
bufferStream.end();
// ERROR ENDS HERE.
});
const newFileName: string = (("somne-prefix_" +
_data.subtitle.replace("/", "")) as string)
.split(" ")
.join("_")
.replace(/-/g, "_")
.replace("pdf", "docx");
return newFileName;
}
}