頁面中組件的引用以及控件觸發事件:
<table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="9" background="images/tab_12.gif"> <asp:Button ID="btnuploadsave" runat="server" OnClientClick="return SaveDoc()" Text="保存" Style="width: 70px; height: 24px; border: 1px solid #c2e1ef; margin-top: 5px;" /> </td> <td bgcolor="e5f1d6"> <table width="99%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CECECE"> <tr> <td width="1360px" height="800px"> <script src="js/LoadWebOffice.js" type="text/javascript"></script> </td> </tr> </table> </td> <td width="9" background="images/tab_16.gif"> </td> </tr>
頁面中js方法:
// -----------------------------== 保存文檔 ==------------------------------------ // function SaveDoc() { var ID = document.getElementById("hfdid").value; var DocType = document.getElementById("ddltype").value; var title = document.getElementById("txttile").value; if(document.getElementById("txttile").value ==""){ alert("標題不可為空") document.getElementById("txttile").focus(); return false; } //恢復被屏蔽的菜單項和快捷鍵 document.all.WebOffice1.SetToolBarButton2("Standard",1,3); document.all.WebOffice1.SetToolBarButton2("Standard",2,3); document.all.WebOffice1.SetToolBarButton2("Standard",3,3); document.all.WebOffice1.SetToolBarButton2("Standard",6,3); if (DocType == "doc") { //恢復文件菜單項 document.all.WebOffice1.SetToolBarButton2("Menu Bar",1,4); //恢復 保存快捷鍵(Ctrl+S) document.all.WebOffice1.SetKeyCtrl(595,0,0); //恢復 打印快捷鍵(Ctrl+P) document.all.WebOffice1.SetKeyCtrl(592,0,0); } else if(DocType == "xls") { //恢復文件菜單項 document.all.WebOffice1.SetToolBarButton2("Worksheet Menu Bar",1,4); } //初始化Http引擎 document.all.WebOffice1.HttpInit(); //添加相應的Post元素 if(ID != ""){ document.all.WebOffice1.SetTrackRevisions(0); document.all.WebOffice1.ShowRevisions(0); document.all.WebOffice1.HttpAddPostString("DID", ID); } document.all.WebOffice1.HttpAddPostString("DocTitle", encodeURI(title)); document.all.WebOffice1.HttpAddPostString("DocType", encodeURI(DocType)); //把當前文檔添加到Post元素列表中,文件的標識符?DocContent document.all.WebOffice1.HttpAddPostCurrFile("DocContent",""); var vtRet = document.all.WebOffice1.HttpPost(document.getElementById("hfurl").value + "/Upload.aspx"); if ("succeed" == vtRet) { alert("文件上傳成功"); }else{ alert("文件上傳失敗"); } return return_onclick(); }
// -----------------------------== 返回首頁 ==------------------------------------ //
function return_onclick() {
document.all.WebOffice1.Close();
window.location.href = "Default.aspx";
return false;
}
Upload.aspx上傳文檔處理頁面為空白頁面,后台代碼如下:
protected void Page_Load(object sender, EventArgs e) { string url = "http://" + Request.ServerVariables["HTTP_HOST"].ToString() + Request.ServerVariables["PATH_INFO"].ToString(); //獲得URL的值 int i = url.LastIndexOf("/"); url = url.Substring(0, i); Response.Clear(); //ID為文檔的主鍵,如果ID不為空,則更新數據,否則新建一條記錄 string ID = Request.Params["DID"], DocTitle = HttpUtility.UrlDecode(Request.Params["DocTitle"]), DocType = HttpUtility.UrlDecode(Request.Params["DocType"]); if (DocType == "") DocType = "doc"; FileEntity docmodel = new FileEntity(); if (ID != "" && ID != null) { docmodel = DocDAL.GetFObj(ID); } DocType = DocType.Substring(0, 3); if (Request.Files.Count > 0) { HttpPostedFile upPhoto = Request.Files[0]; string fileName = Path.GetFileName(upPhoto.FileName); string fname = DateTime.Now.ToString("yyyyyMMddHHmmss") + "." + DocType; if (fileName != null) { string uurl = MapPath("doc/") + fname; upPhoto.SaveAs(uurl); } docmodel.Did = Convert.ToInt32(ID); docmodel.Docurl = url + ("/doc/") + fname; docmodel.Doctitle = DocTitle; docmodel.Doctype = DocType; docmodel.Docdate = DateTime.Now; docmodel.Docstate = "1"; int add = DocDAL.AddFile(docmodel); if (add > 0) { Response.Write("succeed"); } else { Response.Write("failed "); } } else { Response.Write("No File Upload!"); }
Response.End();
}
封裝文件實體類的部分代碼:
/// <summary> /// 文檔實體 ///</summary> public FileEntity() { } private int did;//ID private string doctitle;//標題 private string doctype;//文檔類型 private DateTime docdate;//日期 private string docurl;//保存地址 private string docstate;//狀態
如果遇到瀏覽器不兼容問題,去點聚的官網論壇有解放方法,可以下載相關文件,地址如下:
http://forum.dianju.cn/viewtopic.php?f=3&t=1041