. Net Core Integrated Office Web Apps (i)

Source: Internet
Author: User

Recently started learning. Net Core and using the Visual Studio Code tool to develop. Feeling developed particularly convenient, but there is a headache: many of the library has been modified, womb also familiar with, need to check the official API ...

Microsoft Office Web Apps (OWA) is a web-based online Office tool launched by Microsoft that extends the experience of Microsoft Office products to a supported browser. OWA allows you to share your Office documents anywhere.

System integration OWA needs to refer to the official System interface definition (https://wopi.readthedocs.io/en/latest/), which is referred to as Wopi (Web application Open Platform Interface).

The following is an example of a complete OWA, the final effect

The workflow of the Wopi protocol is as follows

All we have to do is develop an OWA client, provide file information and file streams to the OWA server, and of course receive the file stream from the OWA service post to save the file, and for security reasons you can add Access_token to customize permission validation.

Standard Wopi services include: Checkfileinfo, GetFile, Lock, Getlock, Refreshlock, Unlock, Unlockandrelock, Putfile, Putrelativefile, RenameFile, DeleteFile, Putuserinfo, etc.

Returns the status code definition:

OK success

Error request

401 Unauthorized illegal and access_token related

409 Conflict conflict target file already exists or lock

413 Request Entity Too Large file is too large

$ Internal Server error Internal server errors

501 Not implemented is not supported, and if Checkfileinfo's supportsupdate and usercannotwriterelative are set to True, you must return 501

Owafileinfo class

You need to define a file information class, the main properties of which are:

Property name

Type

Describe

Basefilename

String

File name containing the extension

Breadcrumbfoldername

String

Folder name (interface display)

Breadcrumbdocname

String

Document name (interface display)

ownerID

String

Uniquely identifies the file owner

Size

Long

File size

SHA256

String

SHA-2 256-bit hash coded value

Version

String

Version number

Supportsupdate

Bool

Whether to support put file

Usercanwrite

Bool

Whether you have permission to modify

Supportslocks

Bool

Whether lock and unlock are supported

Closebuttoncloseswindow

Bool

Whether to show the Close button

More Property Reference API https://wopirest.readthedocs.io/en/latest/files/CheckFileInfo.html

The property name must be consistent with the API to be recognized by the OWA server.

usingSystem;usingSystem.Runtime.Serialization;namespacewebapplication.models.fileinfomodels{[DataContract (Name="Owafileinfo")]     Public classOwafileinfo { PublicOwafileinfo () { This. Supportsupdate =false;  This. Usercanwrite =false;  This. Supportslocks =false; } [DataMember (Name="Basefilename")]         Public stringBasefilename {Get;Set; } [DataMember (Name="ownerID")]         Public stringownerID {Get;Set; } [DataMember (Name="Size")]         Public LongSize {Get;Set; } [DataMember (Name="SHA256")]         Public stringSHA256 {Get;Set; } [DataMember (Name="Version")]         Public stringVersion {Get;Set; } [DataMember (Name="supportsupdate")]         Public BOOLsupportsupdate {Get;Set; } [DataMember (Name="Usercanwrite")]         Public BOOLUsercanwrite {Get;Set; } [DataMember (Name="Supportslocks")]         Public BOOLSupportslocks {Get;Set; } [DataMember (Name="Breadcrumbdocname")]         Public stringBreadcrumbdocname {Get;Set; } [DataMember (Name="Closebuttoncloseswindow")]         Public BOOLClosebuttoncloseswindow {Get;Set; } [DataMember (Name="Breadcrumbfoldername")]         Public stringBreadcrumbfoldername {Get;Set; } }}
OwafileinfoCheckfileinfo Service

The interface requires the implementation of the Checkfileinfo service, in which the OWA server needs to obtain the file details and operational permissions (such as whether it can be edited) to ensure that the file is true and valid. This information is already defined in the class above.

Method:get

Uri:http://server/<...>/wopi*/files/<id>

Request Headers:

X-wopi-sessioncontext Context Session parameter value

The interface needs to return the JSON format as follows:

{"Basefilename": "Test.docx", "ownerID": "admin", "Size": 798, "SHA256": "WLBRK+XNDLTHNAOCXNEJBIVZHPHAZZI+1MHKNHUCVLW = "," Version ":" 2016-03-17t02:27:33 "," Supportsupdate ":true," Usercanwrite ":true," Supportslocks " :true, "webeditingdisabled":false}

Description

0. The returned attribute names are case-sensitive, and many plug-ins default to lowercase letters, resulting in docking failure.

1. All OWA Client interface URLs must start with/wopi

Such as:

Http://localhost:5000/api/wopi/files/test.docx

Http://localhost:5000/api/wopi_test/files/test.docx

2. Calculation of SHA256

A. Get the file stream for the file

b SHA256 calculating the file stream hash value

C. Convert the hash value to base64string

string "" ; using (FileStream stream = file.openread (fileName)) using (var sha = SHA256. Create ()) {      byte[] checksum = Sha.computehash (stream);       = convert.tobase64string (checksum);}

3. Verify the result returned by the action, access the Http://localhost:5000/api/wopi/files/test.docx, and return the result as

GetFile Service

When Office Web Apps finishes verifying file information, it's ready to get the file.

Method:get

Uri:http://server/<...>/wopi*/files/<id>/contents?access_token=<token>

Request Headers:

X-wopi-maxexpectedsize

Response Headers:

X-wopi-itemversion file version numbers are similar to versions in Checkfileinfo

Returns the binary stream of a file

[Route ("files/{name}/contents")][httpgetattribute] PublicFilestreamresult Get (stringNamestringAccess_token) {      varFile ="files/"+name; varstream =NewFileStream (file, FileMode.Open, FileAccess.Read); return NewFilestreamresult (Stream,"Application/octet-stream");}
Putfile Service

Method:post

Uri:http://server/<...>/wopi*/files/<id>/contents?access_token=<token>

Request Headers:

X-wopi-override fixed value PUT, must be

X-wopi-lock the string identifier of the lock request (specifically reference to lock request)

Response Headers:

X-wopi-lock the string identity of the lock request

X-wopi-lockfailurereason Lock Failure Reason

X-wopi-itemversion Version number

Request content is a binary format for the file

[Route ("files/{name}/contents")][httppostattribute]  Public Async void Post (stringstring  access_token) {      using (FileStream fs = System.IO.File.Create ("files/" + name)     {          await Request.Body.CopyToAsync (fs);}      }

The basic Office Online preview feature is now basically complete

Follow-up work:

1. Specification request and return header information, perfect function for online editing

2. Introduction of configuration files and cache configuration data

3. Add preview and edit links to the Controller

4. Perfecting Access_token Verification Logic

. Net Core Integrated Office Web Apps (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.