Asp.net MVC 开发对接 Office Online Server

本文章对接接口开发参考:
 
 
 routes.MapRoute(
               name: "filesGet",
               url: "wopi/files/{id}",
               defaults: new { controller = "openapi", action = "filesGet", id = UrlParameter.Optional }
           );
            routes.MapRoute(
               name: "filesPost",
               url: "wopi/files/{id}/contents",
               defaults: new { controller = "openapi", action = "files", id = UrlParameter.Optional }
           ); 
新增路由

 

public class openapiController : Controller
    { 
        /// <summary>
        /// officeserver 获取文件内容Get、回传修改后的文件内容Post
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult files(string id)
        { 
            if (Request.RequestType.ToUpper() == "GET")
            {
                string FilePath = Server.MapPath("~/App_Data/"+ id);
                byte[] data = System.IO.File.ReadAllBytes(FilePath); 
                return File(data, @"application/octet-stream", id); 

            }
            else
            {
                byte[] b = new byte[Request.InputStream.Length];
                Request.InputStream.Read(b, 0, b.Length);
                // 设置当前流的位置为流的开始 
                Request.InputStream.Seek(0, SeekOrigin.Begin);
                string FilePath = Server.MapPath("~/App_Data/" + id);

                using (FileStream fs = System.IO.File.Create(FilePath))
                {
                    fs.Write(b,0,b.Length);
                }
                return Content("OK");

            }

        }

        /// <summary>
        /// 校验文件信息获取合法性
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult filesGet(string id)
        { 
            WopiCheckFileInfo cfi = new WopiCheckFileInfo();
            string FilePath = Server.MapPath("~/App_Data/"+id);
            byte[] data = System.IO.File.ReadAllBytes(FilePath);
            FileInfo f = new FileInfo(FilePath);
            cfi.BaseFileName = id;
            cfi.OwnerId = "Anonymous";
            cfi.UserFriendlyName = "Anonymous"; 
            cfi.Size = data.Length; 
            cfi.Version = DateTime.Now.ToString("s");
            cfi.SupportsCoauth = false;
            cfi.SupportsCobalt = false;
            cfi.SupportsFolders = true;
            cfi.SupportsLocks = true;
            cfi.SupportsScenarioLinks = false;
            cfi.SupportsSecureStore = false;
            cfi.SupportsUpdate = true;
            cfi.UserCanWrite = true;
            return this.Json(cfi, JsonRequestBehavior.AllowGet); 
        }


    }
    
    
    [DataContract]
    public class WopiCheckFileInfo
    {
        [DataMember]
        public bool AllowExternalMarketplace { get; set; }
        [DataMember]
        public string BaseFileName { get; set; }
        [DataMember]
        public string BreadcrumbBrandName { get; set; }
        [DataMember]
        public string BreadcrumbBrandUrl { get; set; }
        [DataMember]
        public string BreadcrumbDocName { get; set; }
        [DataMember]
        public string BreadcrumbDocUrl { get; set; }
        [DataMember]
        public string BreadcrumbFolderName { get; set; }
        [DataMember]
        public string BreadcrumbFolderUrl { get; set; }
        [DataMember]
        public string ClientUrl { get; set; }
        [DataMember]
        public bool CloseButtonClosesWindow { get; set; }
        [DataMember]
        public string CloseUrl { get; set; }
        [DataMember]
        public bool DisableBrowserCachingOfUserContent { get; set; }
        [DataMember]
        public bool DisablePrint { get; set; }
        [DataMember]
        public bool DisableTranslation { get; set; }
        [DataMember]
        public string DownloadUrl { get; set; }
        [DataMember]
        public string FileSharingUrl { get; set; }
        [DataMember]
        public string FileUrl { get; set; }
        [DataMember]
        public string HostAuthenticationId { get; set; }
        [DataMember]
        public string HostEditUrl { get; set; }
        [DataMember]
        public string HostEmbeddedEditUrl { get; set; }
        [DataMember]
        public string HostEmbeddedViewUrl { get; set; }
        [DataMember]
        public string HostName { get; set; }
        [DataMember]
        public string HostNotes { get; set; }
        [DataMember]
        public string HostRestUrl { get; set; }
        [DataMember]
        public string HostViewUrl { get; set; }
        [DataMember]
        public string IrmPolicyDescription { get; set; }
        [DataMember]
        public string IrmPolicyTitle { get; set; }
        [DataMember]
        public string OwnerId { get; set; }
        [DataMember]
        public string PresenceProvider { get; set; }
        [DataMember]
        public string PresenceUserId { get; set; }
        [DataMember]
        public string PrivacyUrl { get; set; }
        [DataMember]
        public bool ProtectInClient { get; set; }
        [DataMember]
        public bool ReadOnly { get; set; }
        [DataMember]
        public bool RestrictedWebViewOnly { get; set; }
        [DataMember]
        public string SHA256 { get; set; }
        [DataMember]
        public string SignoutUrl { get; set; }
        [DataMember]
        public long Size { get; set; }
        [DataMember]
        public bool SupportsCoauth { get; set; }
        [DataMember]
        public bool SupportsCobalt { get; set; }
        [DataMember]
        public bool SupportsFolders { get; set; }
        [DataMember]
        public bool SupportsLocks { get; set; }
        [DataMember]
        public bool SupportsScenarioLinks { get; set; }
        [DataMember]
        public bool SupportsSecureStore { get; set; }
        [DataMember]
        public bool SupportsUpdate { get; set; }
        [DataMember]
        public string TenantId { get; set; }
        [DataMember]
        public string TermsOfUseUrl { get; set; }
        [DataMember]
        public string TimeZone { get; set; }
        [DataMember]
        public bool UserCanAttend { get; set; }
        [DataMember]
        public bool UserCanNotWriteRelative { get; set; }
        [DataMember]
        public bool UserCanPresent { get; set; }
        [DataMember]
        public bool UserCanWrite { get; set; }
        [DataMember]
        public string UserFriendlyName { get; set; }
        [DataMember]
        public string UserId { get; set; }
        [DataMember]
        public string Version { get; set; }
        [DataMember]
        public bool WebEditingDisabled { get; set; }
    }
Controller Action 以及调用的Model

 

以上链接为请求office online server 打开一个在线文档。

http%3A%2F%2Fsp.dev.website.com%2Fwopi%2Ffiles%2FMEF100a.xlsx  此参数为对应的连接地址;

office online server 在打开时会发起两次Get请求:(请注意此时的请求都是GET)

第一次请求: http://sp.dev.website.com/wopi/files/MEF100a.xlsx  , 对应的路由就是  url: "wopi/files/{id}"  对应的  openapiController.filesGet . 用于验证文件是否可取,服务器端需要返回相关的文件信息

第二次请求: http://sp.dev.website.com/wopi/files/MEF100a.xlsx/contents  ,对应的路由就是  url: "wopi/files/{id}/contents" 对应的   openapiController.files ,获取文件流。

office online server 在存档时,会发起POST请求,把文件内容传回原来的服务器。

POST请求: http://sp.dev.website.com/wopi/files/MEF100a.xlsx/contents  ,   ,对应的路由就是  url: "wopi/files/{id}/contents" 对应的   openapiController.files ,上传文件流。

 

本文代码只是简单的文件获取 ,没有对文件的访问做权限验证、存档权限验证等,若有需要可在Action的访问添加相关参数。

 

 

 

posted @ 2020-11-27 11:22  写代码让我快乐  阅读(401)  评论(0编辑  收藏  举报