资料一:适合读取并显示(简单而明了)
第一种方法:
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "Application/msword";
string s=Server.MapPath("C#语言参考.doc");
Response.WriteFile("C#语言参考.doc");
Response.Write(s);
Response.Flush();
Response.Close();
第二种方法:
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "Application/msword";
string strFilePath="";
strFilePath =Server.MapPath("C#语言参考.doc");
FileStream fs = new FileStream(strFilePath,FileMode.OpenOrCreate,FileAccess.Read);
Response.WriteFile(strFilePath,0,fs.Length);
fs.Close();
第三种方法:
string path=Server.MapPath("C#语言参考.doc");
FileInfo file=new FileInfo(path);
FileStream myfileStream=new FileStream(path,FileMode.Open,FileAccess.Read);
byte[] filedata=new Byte[file.Length];
myfileStream.Read(filedata,0,(int)(file.Length));
myfileStream.Close();
Response.Clear();
Response.ContentType="application/msword";
Response.AddHeader("Content-Disposition","attachment;filename=文件名.doc");
Response.Flush();
Response.BinaryWrite(filedata);
Response.End();
资料二:适合上传WORD并显示与保存
//保存数据到当前客户端(可以传入一个要保存的文件名).
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);}
}
//Word转化为Html文件
function WorcChangeHtml()
{
var os_xmlFSO;
//获得上传控件对象
var objUpFile = window.document.Form1.updFile;
//获得客户端Word文件路径和文件
var UpFilue = window.document.Form1.updFile.value;
if(os__xmlFSO == null)
os__xmlFSO = new ActiveXObject("Scripting.FileSystemObject");
try
{
if(window.document.Form1.updFile.value == "")
{
alert('请选择对应的Word文件');
objUpFile.focus();
}
else if(UpFilue.indexOf(".doc") == -1)
{
alert('您选择的不是Word文件 \r\n请选择正确的Word文件');
objUpFile.focus();
}
else if(!os__xmlFSO.FileExists(objUpFile.value))
{
alert('对应的Word文件不存在');
objUpFile.focus();
}
else
{
var wdFormatHTML = 8;
var objWord = new ActiveXObject("Word.Application");
objWord.Application.Visible = false;
var objDoc = objWord.Documents.Open(UpFilue);
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>");
//将转化后的值赋给页面控件txtIdea的值,我为了将Word值保存进数据库所以用<input type = "hidden" ..... 如果将Word内容显示可以考虑 window.document.Form1."你的显示控件ID".innerText = GetHtml;
window.document.Form1.txtIdea.value = GetHtml;
}
}
catch(e){window.alert(e);}
}
//读取文本文件
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>
注意页面上需要添加以下2个控件和对应的客户端事件
<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="导入" name="btnUpLoad">
<textarea style="WIDTH: 15.25%; HEIGHT: 23px" rows="50" cols="16" id="txtIdea"
runat="server">
其中txtIdea中的值就是客户端Word中的内容了,注意:需要调整IE的安全性设置,否则将无效.