How to do online viewing of documents

  • Tutorial
First work day. The first challenge at Redmine. The first specification is in doc format. On a new working machine. We managed to start reading the specification in about 3 hours. MS Office was downloaded and installed. Remembering this case, I was sure that in our task management system it is necessary to make online viewing of documents. Here are just ideas for implementation in a reasonable amount of time and labor. We recently found a way - Microsoft Office Web Apps.

This article will discuss how to add online document viewing to any product.

General view


Office Web Apps Server (hereinafter OWA) is a web version of the popular office suite of applications, which includes lightweight browser applications Word, Excel, PowerPoint and OneNote.

Available for free download and use in document viewing mode.

You can watch OWA in action on OneDrive . Several documents in read mode:


OWA is installed on a separate physical server and is only responsible for displaying documents in a browser. All the logic for organizing file storage falls on your application. The interaction between OWA and the repository occurs through the WOPI API, which will have to be implemented.
image


It is installed using the wizard and the button NEXT.
After a successful installation at HTTPS: // OWA-server / hosting / discovery there will be an xml description of the supported applications, actions and url access patterns:


...

...

...

...

...

...

...

...

...

...

...

...

There is much hidden behind the dots. But the remaining steps show that both documents in Microsoft Office format and Open Office are supported. In addition to viewing (view), you can open documents and edit (edit). There is a mobile version (mobileView). OWA can generate a preview of the first page of a document as an image (imagepreview) and a preview of all pages of a document (interactivepreview).

Suppose we want to display a document with the extension docx in the Word browser. Find the url template:
https: //OWA-server/wv/wordviewerframe.aspx?
Optional parameters are enclosed in angle brackets (<>):

  • UI_LLCC - preferred language in the OWA interface;
  • DC_LLCC - the preferred language when it can affect the display of the document;
  • Showpagestats - display service statistics.

To this url we need to add the WOPISrc parameter, by which OWA will request a document from our repository, and the access_token parameter, which will be transmitted along with the file request for authorization.

As a result, there will be a URL of the form HTTPS: //OWA-server/wv/wordviewerframe.aspx? WOPISrc = https% 3A% 2F% 2Fmy-wopi-host% 2Fwopi% 2Ffiles% 2FDocument.docx & access_token = 0bf6fe96-3510-4105-ac1cfc1cfc (*)

Wopi


WOPI (Web Application Open Platform Interface) is a RESTful API that defines a set of operations for accessing and modifying files on our file storage. It works through HTTP / HTTPS.

I prepared an example of a simple file storage with the minimum operations necessary for viewing documents from WOPI. The project is on ASP.Net MVC 5. But nothing prevents the use of any other framework and programming language.

We will open in the browser the URL (*) received by us a little earlier.

First of all, OWA will call the CheckFileInfo method available at HTTP address: // server / <...> / wopi * / files /? access_token =.
Method implementation:
[Route("files/{fileName}")]
[HttpGet]
public FileInfoDto CheckFileInfo(string fileName, [FromUri(Name = "access_token")] Guid tokenId)
{
    var fullFileName = GetFullPath(fileName);
    Validation(tokenId, fullFileName);
    return new FileInfoDto
    {
        BaseFileName = fileName,
        OwnerId = "admin",
        ReadOnly = true,
        SHA256 = GetChecksum(fullFileName),
        Size = new FileInfo(fullFileName).Length,
        Version = 1
    };
}
private static string GetChecksum(string filePath)
{
    using (var stream = new BufferedStream(File.OpenRead(filePath), 1200000))
    {
        var checksum = SHA256.Create().ComputeHash(stream);
        return Convert.ToBase64String(checksum);
    }
}


Only required parameters are returned here. This is file information. A number of optional parameters describe, for example, whether the file can be edited and how, information about the author, support for working with folders, information about user rights, whether the file should be protected from copying, etc. All configs are described here .

If the method has successfully completed and returned the correct data, OWA will request the file itself at the address HTTP: // server / <...> / wopi * / files // contents? access_token =.
We give the file:
[Route("wopi/files/{fileName}/contents")]
[HttpGet]
public HttpResponseMessage GetFile(string fileName, [FromUri(Name = "access_token")] Guid tokenId)
{
    var fullFileName = GetFullPath(fileName);
    Validation(tokenId, fullFileName);
    var stream = new FileStream(fullFileName, FileMode.Open);
    var result = new HttpResponseMessage(HttpStatusCode.OK)
    {
        Content = new StreamContent(stream)
    };
    result.Content.Headers.ContentType = new MediaTypeHeaderValue(MimeMapping.GetMimeMapping(fileName));
    result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
    {
        FileName = fileName
    };
    return result;
}


Actually, we wrote the necessary functionality for displaying files!

Iron


The narrowest and most expensive place in this whole idea.

Minimum system requirements:

  • 8 GB of RAM;
  • 64-bit quad-core processor;
  • 80 GB of disk space.

It is claimed that an 8-core server with 8 GB of RAM can withstand a load of 10,000 users. And 16 and nuclear with 16 GB of RAM - 20,000 users.

Supported Operating Systems:

  • 64-bit Windows Server 2008 R2,
  • 64-bit Windows Server 2012 Standard or Datacenter

It is put on a separate physical or virtual server. However, other server applications (for example, MS SQL Server) cannot be installed on the same server.

The server must be part of the domain , otherwise OWA just will not install, I tried. Also, OWA will not work if you install it on a domain controller.

Is it possible to edit documents in a browser?


OWA supports editing, but you can only use document viewing for free. Editing requires a license .

By default, editing mode is turned off. But you can turn it on at any time. Editing PowerPoint and Excel documents works through WOPI. But for Word you have to implement FSSHTTP .

Also popular now: