In the web, how to read the client 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 for operation. Each of these two methods has its own characteristics. Pros and cons.
I will give an example of client operation.
First, add the following js code in .aspx
<script language='javascript'>
//The default word conversion file is placed under C:\
var os__localPath = "C:\";
//saved file name
var os__localFile = "defaultFileWord.htm";
var os__xmlDom = new ActiveXObject("MSXML2.DOMDocument");
var os__xmlFSO;
//Save data to the current client (you can pass in 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;
//Get the upload control object
var objUpFile = window.document.Form1.updFile;
//Get the client Word file path and file
var UpFileValue = window.document.Form1.updFile.value;
if(os__xmlFSO == null)
os__xmlFSO = new ActiveXObject("Scripting.FileSystemObject");
try
{
if(window.document.Form1.updFile.value == "")
{
alert('Please select the corresponding Word file');
objUpFile.focus();
}
else if(UpFileValue.indexOf(".doc") == -1)
{
alert('The file you selected is not a Word filernPlease select the correct Word file');
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);
window.document.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 value of the page control txtIdea. In order to save the Word value into the database, I use <input type = "hidden".... If you want to display the Word content, you can consider window.document.Form1. "Your display control ID".innerText = GetHtml;
window.document.Form1.txtIdea.value = GetHtml;
}
}
catch(e){window.alert(e);}
}
//Read text file
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 that the following 2 controls and corresponding client events need to 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"
onclick="WorcChangeHtml()" 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, otherwise it will be invalid.