vue 纯前端 word 转html

Cola-blog 2024-07-09 09:03:06 阅读 71

vue2   安装   npm install -save mammoth.js ---word转换html插件

html代码  用的vue  ant-design-vue

<code> <a-upload name="file" accept=".pdf,.doc,.docx" :before-upload="beforeUpload">code>

<a-button>

<a-icon type="upload" /> 选择 </a-button>code>

</a-upload>

import mammoth from 'mammoth';

beforeUpload(file){

if (!file) {

return;

}

console.log(file+"input")

let fileType = ['.docx','.doc']

let { name } = file;

let nameLen = name.length;

let hzIndex = name.lastIndexOf(".");

//获取后缀名

let hz = name.substring(hzIndex, nameLen);

if (!fileType.includes(hz)) {

this.$message.warning({

message: "注意:只支持docx格式的文档",

})

return false

}

console.log(hz);

var that = this;

var reader = new FileReader();

reader.readAsArrayBuffer(file);

reader.onload = function (loadEvent) {

console.log(loadEvent.target.result);

var arrayBuffer = loadEvent.target.result;

mammoth.convertToHtml({ arrayBuffer: arrayBuffer })

.then(

function (resultObject) {

setTimeout(function(){

that.agreeContent = resultObject.value

}, 300);

console.log(resultObject.value);//这个就是我们得到的html

}).done();

};

},



声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。