import { convertToHtml } from 'mammoth'; import Vue from 'vue' export default { async convertFileToHtml(url) { var xhr = new XMLHttpRequest(); xhr.open('GET', Vue.prototype.javaApi+url, true);//获取文件流的接口 xhr.send(); xhr.responseType = "blob";//不能漏 let xhrPromise = new Promise((resolve, reject) => { xhr.onload = async function () { if (this.status === 200) { // 返回的文件流,转换成blob对象 var blob = new Blob([this.response],{ type:'application/vnd.openxmlformats-officedocument.wordprocessingml.document' }); // 使用mammoth将Word转换为HTML let reader = new FileReader(); reader.readAsArrayBuffer(blob); let htmlContentPromise = new Promise((resolve, reject) => { reader.onload = async function () { var arrayBuffer = xhr.response; //arrayBuffer const result = await convertToHtml({ arrayBuffer: arrayBuffer }) let html = result.value.replace(//g, '') .replace('
/g, ' | ')
.replace(/ /g, ' |