Asp.net 使用weboffice实现Word在线编辑

项目中客户需要在页面中实现Word的在线编辑,Word写保护等功能。之前写了篇文章,用的是dsoframer.ocx,参考的都是网络上的资源。用的时候页面卡,

而且word2003和word2007有问题。现在找了个另外一个组件,点聚公司的weboffice,还不错。主要是免费的。

关于weboffice的使用,大家可以看看官网,他那有演示。我这也把我找的资源和大家分享下。

关于用户在线编辑后上传文档到服务器,实现方式如下:

js处理:

代码
 

  var Saveinfo = function() {
            try {
                var id = getUrlPara("id");

                var webObj = document.getElementById("WebOffice1");
                webObj.HttpInit();   //初始化Http引擎
                // 添加相应的Post元素
                webObj.HttpAddPostString("id", id);
                var state = "0";
                if (document.getElementById("Checkbox1").checked == true) {
                    state = "1";
                }
                webObj.HttpAddPostString("state", state);
                //  webObj.HttpAddPostString("DocID", myform.DocID.value);
                webObj.HttpAddPostCurrFile("DocContent", "");  // 上传文件
                returnValue = webObj.HttpPost("http://localhost:38706/AssessManage/AssessDocuments/weboffice/savedoc.ashx"); // 判断上传是否成功
                if ("succeed" == returnValue) {
                    alert("修改成功,请更新列表页面!");
                } else if ("failed" == returnValue)
                    alert("修改失败");
            } catch (e) {
                alert("异常\r\nError:" + e + "\r\nError Code:" + e.number + "\r\nError Des:" + e.description);
            }
        }

savedoc.ashx进行文件处理:

这里我是创建一个新的doc文件。

 

代码
public class savedoc : IHttpHandler
    {

        
public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType 
= "text/plain";
            context.Response.Buffer 
= true;
            context.Response.ExpiresAbsolute 
= DateTime.Now.AddDays(-1);
            context.Response.AddHeader(
"pragma""no-cache");
            context.Response.AddHeader(
"cache-control""");
            context.Response.CacheControl 
= "no-cache";

            Super.Wdxt.Kpgl.EnumList.UserIdentity _useridentity 
= IdebtityBll.GetIndentityName(Super.Wdxt.Kpgl.Common.getUserBasicInfo.SchoolId(), Super.Wdxt.Kpgl.Common.getUserBasicInfo.UserId());
            
if (_useridentity != Super.Wdxt.Kpgl.EnumList.UserIdentity.DeputyEnchou)
            {
                context.Response.Write(
"no right!");
            }

            
//ID为文档的主键,如果ID不为空,则更新数据,否则新建一条记录
            string ID = context.Request.Params["ID"];
            
if (context.Request.Files.Count > 0)
            {
                HttpPostedFile upPhoto 
= context.Request.Files[0];
                
int upPhotoLength = upPhoto.ContentLength;
                
byte[] PhotoArray = new Byte[upPhotoLength];
                Stream PhotoStream 
= upPhoto.InputStream;
                PhotoStream.Read(PhotoArray, 
0, upPhotoLength); //这些编码是把文件转换成二进制的文件
                string Newfilename = Super.Wdxt.Kpgl.Common.Utils.NewName("")+"_"+Super.Wdxt.Kpgl.Common.getUserBasicInfo.UserId()+".doc";
                
string path = System.Configuration.ConfigurationManager.AppSettings["DocumentsPath"].ToString();
                
if (!File.Exists(Newfilename))
                {
                    FileStream fs 
= new System.IO.FileStream(path+Newfilename, System.IO.FileMode.CreateNew, System.IO.FileAccess.Write);
                    fs.Write(PhotoArray, 
0, PhotoArray.Length);
                    fs.Close();
                }
                
string state = context.Request.Params["state"];
                
bool flag=Super.Wdxt.Kpgl.BLL.AssessdocumentsBLL.Edit(ID, Newfilename, state);
                
if (flag)
                {
                    context.Response.Write(
"succeed");
                  }
                
else
                {
                    context.Response.Write(
"failed");
                }
               context.Response.End();

                
//-------------------------------------------
            }
            
else
            {
                context.Response.Write(
"No File Upload!");
            }
        }

        
public bool IsReusable
        {
            
get
            {
                
return false;
            }
        }
    }

 

  相关实例: 1。ex1 

                2.ex2

 

 

 

 

 

posted @ 2010-07-03 14:15  clound  阅读(10622)  评论(1编辑  收藏  举报