On the web, how does one read the client's word content and display it on the page?
I found that there are two methods: one is to operate directly on the client, and the other is to upload the client's word to the server, which have their own advantages and disadvantages.
Here is an example of client operations.
Add the following JS Code in. aspx:
<Script language = 'javascript '>
// The default word conversion file is stored in C: //
VaR OS _ localpath = "C ://";
// Saved file name
VaR OS _ localfile = "defaultfileword.htm ";
VaR OS _ xmldom = new activexobject ("msxml2.domdocument ");
VaR OS _ xmlfso;
// Save the data to the current client (you can input a file name to be saved ).
Function OS _savetolocal ()
{
VaR _ saveas = "";
If (arguments. length> 0)
_ Saveas = arguments [0] + "";
Else
_ Saveas = OS _ localfile;
Try
{
If (OS _ xmlfso = NULL)
OS _ xmlfso = new activexobject ("scripting. FileSystemObject ");
}
Catch (e) {window. Alert (E );}
}
// Convert Word to HTML file
Function worcchangehtml ()
{
VaR OS _xmlfso;
// Obtain the upload control object
VaR objupfile = Invalid upload Doc ument. form1.updfile;
// Obtain the client's Word file path and file
VaR upfilevalue = specified parameter Doc ument. form1.updfile. value;
If (OS _ xmlfso = NULL)
OS _ xmlfso = new activexobject ("scripting. FileSystemObject ");
Try
{
If(window.doc ument. form1.updfile. value = "")
{
Alert ('select the corresponding word file ');
Objupfile. Focus ();
}
Else if (upfilevalue. indexof (". Doc") =-1)
{
Alert ('the file you selected is not a Word file/R/n Please select the correct wordfile ');
Objupfile. Focus ();
}
Else if (! OS _ xmlfso. fileexists (objupfile. Value ))
{
Alert ('the corresponding Word file does not exist ');
Objupfile. Focus ();
}
Else
{
VaR wdformathtml = 8;
VaR objword = new activexobject ("word. application ");
Objword. application. Visible = false;
VaR objdoc = objword. Documents. Open (upfilevalue );
Objdoc. saveas (OS _ localpath + OS _ localfile, wdformathtml );
Required parameter Doc ument. form1.updfile. value = "";
Objdoc. Close ();
Objword. Quit ();
VaR gethtml = Getline ();
VaR ibeginindex = gethtml. indexof ("<body ");
VaR iendindex = gethtml. lastindexof ("</body> ");
Gethtml = gethtml. substring (ibeginindex, iendindex + 7). Replace ("<body", "<Div ");
Gethtml = gethtml. Replace ("</body>", "</div> ");
// Assign the converted value to the txtidea value of the page control. To save the word value to the database, use <input type = "hidden "..... if you want to display the word content, consider using your own Doc ument. form1. "your display control ID ". innertext = gethtml;
Required parameter document.form1.txt idea. value = gethtml;
}
}
Catch (e) {window. Alert (E );}
}
// Read text files
Function Getline ()
{
VaR FSO, txtfile, strvalue;
VaR forreading = 1, forwriting = 2;
FSO = new activexobject ("scripting. FileSystemObject ");
Txtfile = FSO. opentextfile (OS _ localpath + OS _ localfile, forreading );
While (! Txtfile. atendofstream)
{
Strvalue = strvalue + txtfile. Readline ();
}
Txtfile. Close ();
Return (strvalue );
}
</SCRIPT>
Note: The following two controls and corresponding client events must be added to the page.
<Input id = "updfile" type = "file" style = "border-Right: 1px solid; border-top: 1px solid; border-left: 1px solid; width: 77.46%; border-bottom: 1px solid; Height: 26px"
Size = "71"> <input style = "border-Right: #999999 1px solid; border-top: #999999 1px solid; font-size: 15pt; border-left: #999999 1px solid; width: 103px; border-bottom: #999999 1px solid; Height: 28px"
Runat = "server" id = "btnupload" type = "Submit" value = "import" name = "btnupload">
<Textarea style = "width: 15.25%; Height: 23px" rows = "50" Cols = "16" id = "txtidea"
Runat = "server">
The value in txtidea is the content in the client word. Note: You need to adjust the Security Settings of IE.