LoadWebOffice实现在线编辑Word
1.准备工作
- (1)LoadWebOffice.js 提取码:iyk4
- (2)weboffice.ocx 提取码:nde6
2.代码块
LoadWebOffice目前适用于IE浏览器,火狐和谷歌浏览器要想使用需要安装Firefox ActiveX Plugin,下面的LoadWebOffice.js内容代码火狐和谷歌部分因需求变动未进行测试。
- (1).LoadWebOffice.js内容
var s = "";
if(navigator.userAgent.indexOf("MSIE")>0){
s = "<OBJECT id='WebOffice1' align='middle' style='LEFT: 0px; WIDTH: 100%; TOP: 0px; HEIGHT:768px'"
+ "classid=clsid:E77E049B-23FC-4DB8-B756-60529A35FAD5> codebase='Common/weboffice.ocx#Version=5,0,0,0'"
+ "</OBJECT>";
}
if(navigator.userAgent.indexOf("Chrome")>0){
s = "<object id='WebOffice1' type='application/x-itst-activex' align='baseline' border='0'"
+ "style='LEFT: 0px; WIDTH: 100%; TOP: 0px; HEIGHT: 768px'"
+ "clsid='{E77E049B-23FC-4DB8-B756-60529A35FAD5}' codebase='Common/weboffice.ocx#Version=5,0,0,0'"
+ "event_NotifyCtrlReady='WebOffice1_NotifyCtrlReady'>"
+ "</object>";
}
if(navigator.userAgent.indexOf("Firefox")>0){
s = "<object id='WebOffice1' type='application/x-itst-activex' align='baseline' border='0'"
+ "style='LEFT: 0px; WIDTH: 100%; TOP: 0px; HEIGHT: 768px'"
+ "clsid='{E77E049B-23FC-4DB8-B756-60529A35FAD5}' codebase='Common/weboffice.ocx#Version=5,0,0,0'"
+ "event_NotifyCtrlReady='WebOffice1_NotifyCtrlReady'>"
+ "</object>";
}
document.write(s)
- (2).js代码
<script language="javascript" event="NOtifyCtrlReady" for="WebOffice1">
WebOffice1_NotifyCtrlReady();
</script>
<script>
function WebOffice1_NotifyCtrlReady() {
$.ajax({
async: false,
type: "post",
data: {
action: ""
},
url: "",
success: function (data) {
path = data.FiledKey;
document.all.WebOffice1.LoadOriginalFile(path,"doc");
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
layer.alert('请求发生异常!', { icon: 3 });
}
});
}
//关闭页面时,关闭文件
function window_onunload() {
document.all.WebOffice1.Close();
}
//个人需求保存方法
function upload() {
document.all.WebOffice1.HttpInit(); //初始化Http引擎
// 添加相应的Post元素
document.all.WebOffice1.HttpAddPostString("DocTitle", "test1");
document.all.WebOffice1.HttpAddPostString("DocID", "");
document.all.WebOffice1.HttpAddPostString("DocType", "doc");
document.all.WebOffice1.HttpAddPostCurrFile("DocContent", ""); // 添加文件
//请求路径
var result = document.all.WebOffice1.HttpPost("");
if ("succeed" == result) {
alert("文件上传成功");
} else
alert("文件上传失败")
return_onclick();
}
</script>
//HTML代码
<body onunload="return window_onunload()">
<form id="form1" runat="server">
<input type="button" onclick="return upload()" class="layui-btn layui-btn-xs" value="上传文档"/>
<br />
<br />
<script src="../jscss/js/LoadWebOffice.js"></script>
</form>
</body>