我对这个功能的理解是:拿到你想要显示的信息,写入到一个临时文件当中去以便插件进行编辑。把临时文件的相对路径传到页面去。页面拿到这个要显示的文档的相对路径对文档进行编辑在Word文档里面显示出来
后台代码部分:
@RequestMapping("check")
public String check(String wid,Model model,HttpServletResponse response,HttpServletRequest request) throws IOException{
UUidUtil uuid = new UUidUtil();
String uid = uuid.getUid();//去掉"-"的32位uuid
String fileName = "";//文件名称
String fileUrl = "";//文件路径
String absoluteOfficeFileDir = request.getServletContext().getRealPath("/")+ "resources/uploadOfficeFile"; //office文件的绝对路径
String relativeOfficeFileUrl = "../resources/uploadOfficeFile"; //office文件访问的相对路径
//读取内容写入临时文件,以便插件在线编辑
byte fileBody[] = new byte[10240];
Words word = wordsBiz.getWordByWid(wid);
fileName = word.getName()+".docx";
fileBody = word.getDesc().getBytes();
fileUrl = relativeOfficeFileUrl+"/"+uid+fileName;//相对路径,访问用
String inFileUrl= absoluteOfficeFileDir+ "/" +uid+fileName;//绝对路径,io操作所用
FileOutputStream fs = new FileOutputStream(inFileUrl);
fs.write(fileBody);
fs.flush();
fs.close();
model.addAttribute("fileUrl", fileUrl);
model.addAttribute("word", word);
return "wordscheck";
}
前台页面部分:
pageEncoding="UTF-8"%>
office文档编辑文 件 ID: | 文件名称: |
//重点的JS 必须要加载的
var OFFICE_CONTROL_OBJ;//控件对象
var IsFileOpened; //控件是否打开文档
var fileType;
var fileTypeSimple;
function load(fileUrl){
alert(fileUrl);
OFFICE_CONTROL_OBJ = document.all("TANGER_OCX");//浏览文档控件的加载必须要有的
NTKO_OCX_OpenDoc(fileUrl);//需要的内容加载进文档控件
}
function NTKO_OCX_OpenDoc(fileUrl) {
OFFICE_CONTROL_OBJ.BeginOpenFromURL(fileUrl);
}
《ntkoofficecontrol.js》重点代码
document.write(' ');
document.write(' ');
document.write('
document.write('codebase="http://www.ntko.com/control/officecontrol/OfficeControl.cab#version=5,0,2,7" width="100%" height="100%"> ');
document.write(' ');
document.write(' ');
document.write(' ');
document.write(' ');
document.write(' ');
document.write(' ');
document.write(' ');
document.write(' ');
document.write(' ');
document.write(' ');
document.write(' ');
document.write(' ');
document.write('不能装载文档控件。请确认你可以连接网络或者检查浏览器的选项中安全设置。安装演示产品 ');
document.write('
');