1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
<script language="javascript"> function insertDoc(filename) { var tmpfile = "c://temp.html"; // check template file if exsited checkFile(tmpfile); var word = new ActiveXObject("Word.Application"); word.visible = false; var doc = word.documents.open(filename, false); if (doc != null) { doc.saveAs(tmpfile, 10); // convert doc to html,请注意如果是Word2000请将参数改为8,word2003为10 doc.close(); } word.quit(); //get images'path from word,then put them into inputbox putImages(tmpfile); } function checkFile(filename) { var fso = new ActiveXObject("Scripting.FileSystemObject"); if (fso.FileExists(filename)) { fso.DeleteFile(filename, true); } } function putImages(filename) { // get data from template file var tmpstr = ""; var fso1 = new ActiveXObject("Scripting.FileSystemObject"); if (fso1.FileExists(filename)) { var txtstrm = fso1.OpenTextFile(filename, 1); tmpstr = txtstrm.readAll(); txtstrm.close(); } // 下面一大段注释掉的代码是为了下次提取word文档中图片的路径用的,现在还用不到。 /** var tmp = ""; var ie = new ActiveXObject("InternetExplorer.Application"); ie.Navigate(filename); while (ie.ReadyState != 4) {} var objs = ie.Document.all; var l = objs.length; var tmptag = ""; var tmpsrc = ""; var body_top = "", body_buttom = "", body_mid = ""; document.writeln("选择文件上传"); body_top = "<table border=1><form name='upladofiles' method='post' action='uploadfiles.jsp'><tr><td>"; document.writeln(body_top); for (i = 0; i < l; i++) { var obj = objs[i]; try { tmptag = obj.tagName.toLowerCase(); if (tmptag == "img") { tmpsrc = obj.src; tmpsrc = tmpsrc.substring(8, tmpsrc.length); body_mid = "<tr><td><input type='file' name='f' id='>" + i + "'></td>"; body_mid = body_mid + "<td>" + tmpsrc + "</td></tr>"; //document.writeln(body_mid); continue; } } catch (e) {} } var tmpcontents = "<tr><td><textarea name='txtDoc' width=150>" + tmpstr + "</textarea></td></tr>"; document.writeln(tmpcontents); document.writeln("<tr><td><input type='submit' name='confirm' value='确定'></td></tr></td></tr></form></table>"); document.writeln("<table><tr><td><iframe name='fr_editor' id='fr_editor' width='717' height='501'></iframe></td></tr></table>"); alert(tmpstr); */ ScriptDiv.focus(); var sel = ScriptDiv.document.selection.createRange(); sel.pasteHTML(tmpstr); fso1.DeleteFile(filename, true); //ie.Quit(); } </script> 好了具体的代码写完了,要测试就要写一个网页 <INPUT type=file name=file1> <INPUT type=button value=ok name=ok LANGUAGE=javascript onclick="return ok_onclick()"> </p><p> <iframe name="ScriptDiv" id="ScriptDiv" width="633" height="1968" ></iframe> </p> |