我正在寻找一种在HTML页面上显示Word文件的解决方案,我找到了以下代码,这正是我想要的。
我唯一的问题是: 您必须在此处上传Word 文件,然后单击按钮才能显示该文件。
也就是说,我不想点击任何按钮!
例如,您输入以下链接:mysite.com/articles?id=5
我在数据库中有一个表,里面保存了Word文件的地址,例如ID 5,保存了以下地址:
文件/文章/new-method-in-raise-children.docx
我想在页面加载时将其显示在div标签中。
<html>
<head>
<title>display word file</title>
</head>
<body>
<input id="files" type="file" accept=".docx"/>
<input type="button" id="btnPreview" value="Preview Word Document" onclick="PreviewWordDoc()"/>
<div id="word-container" class=""></div>
<script type="text/javascript" src="https://unpkg.com/jszip/dist/jszip.min.js"></script>
<script src="Scripts/docx-preview.js"></script>
<script type="text/javascript">
function PreviewWordDoc() {
//Read the Word Document data from the File Upload.
var doc = document.getElementById("files").files[0];
//If Document not NULL, render it.
if (doc != null) {
//Set the Document options.
var docxOptions = Object.assign(docx.defaultOptions, {
useMathMLPolyfill: true
});
//Reference the Container DIV.
var container = document.querySelector("#word-container");
//Render the Word Document.
docx.renderAsync(doc, container, null, docxOptions);
}
}
</script>
</body>
</html>
我找到了解决这个问题的更简单更好的方法: