onlyoffice tutorial 0x04 file saving

Keywords: office

File saving process

The file saving process of onlyoffice is as follows

  • The user edits the document in the document editor
  • The document editor sends the changes to the document server
  • When the user closes the document editor, the document server will call back the callback URL configured by the user for notification
  • The callback program downloads the latest document and saves it to the document server

realization

code

First, as in the previous article, create html files locally

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>onlyoffice course</title>
</head>
<body>
<div id="placeholder"></div>
<script type="text/javascript" src="http://47.113.219.133:9001/web-apps/apps/api/documents/api.js"></script>
<script>
    var docEditor = new DocsAPI.DocEditor("placeholder", {
        "document": {
            "fileType": "doc",
            "permissions": {
                "edit": true,
            },
            "key": "100000",
            "title": "Test page.docx",
            "url": "https://zhengjianfeng.cn/document/example.docx",
        },
       "editorConfig":{
            "lang":"zh-CN",
                 "callbackUrl": "http://47.113.219.133:8080/api/office/callback",
            "user":{
                "group":"admin",
                "id":"004",
                "name":"Jian Feng Zheng"
            }
        },
        "height": "800px",
        "width": "100%"
    });

</script>
</body>
</html>

callbackUrl specifies the callback interface

The document server will call back the interface many times during document editing and send the user's current editing status to the callback interface. For example, when the user opens a document for editing, it will call back the interface and send the following data

{
    "key": "100000",
    "status": 1,
    "users": [
        "004"
    ],
    "actions": [
        {
            "type": 1,
            "userid": "004"
        }
    ]
}

Status indicates the editing status of the user. There are the following statuses:

  • 1 - the document is being edited, which is the state when the general user opens the document,
  • 2 - the document is about to be saved by the document server, and the user finishes editing and closes the editor,
  • 3 - Document saving failed
  • 4 - document content unchanged
  • 6 - the user manually clicks save, which requires additional configuration
  • 7 - user manual save failed

Generally, we only need to focus on the 2 and 6 states. We only concern the saved document. What is the difference between the two states? By default, when only the user closes the document editor, the document server will call back the 2 state to the interface after about 10s. Therefore, the delay of 10s depends on the file size and the conversion time, Because the background of onlyoffice needs to convert documents to OPEN XML format, there are two solutions. One is to modify the configuration file to reduce the delay time (this is not recommended). You can edit the configuration file documentserver/default.json

{
    "services": {
        "CoAuthoring": {
            "server": {
                "savetimeoutdelay": 5000
            }
        }
    }
}

The other is to enable the forced save function, that is, when the user clicks save, the interface will be called immediately to modify the editor configuration and set forcesave to true

"editorConfig": {
        "lang": "zh-CN",
        "user": {
            "group": "admin",
            "id": "004",
            "name": "Jian Feng Zheng"
        },
        "callbackUrl": "http://47.113.219.133:8080/api/office/callback",
        "customization": {
            "forcesave": true
        }
    },

When the user saves, onlyoffice will send data with status=6. The complete data is as follows

{
    "key": "100000",
    "status": 6,
    "url": "http://47.113.219.133:9001/cache/files/100000_5043/output.docx/output.docx?md5=XkF44g2sAkkZVDHCFbJ5Ug&expires=1638441611&filename=output.docx",
    "changesurl": "http://47.113.219.133:9001/cache/files/100000_5043/changes.zip/changes.zip?md5=CGdhAG-sO5AUhM3JsKL_Fg&expires=1638441611&filename=changes.zip",
    "history": {
        "serverVersion": "6.4.2",
        "changes": [
            {
                "created": "2021-12-02 10:23:57",
                "user": {
                    "id": "004",
                    "name": "admin Jian Feng Zheng"
                }
            }
        ]
    },
    "users": [
        "004"
    ],
    "actions": [
        {
            "type": 2,
            "userid": "004"
        }
    ],
    "lastsave": "2021-12-02T10:24:55.000Z",
    "notmodified": false
}
  • Key: document primary key
  • url: saved document path
  • Changeurl: onlyoffice will save the changed contents of the document into a zip compressed file. If necessary, you can save the file. You can i display the changed contents on the interface through the relevant api of editor
  • History: document editing history
  • Users: editing users. Since onlyoffice supports collaborative editing, there may be multiple users

callback handler

Onlyoffice will call back the interface through post, so any interface that supports post requests can be used as a callback interface. The callback interface needs to return the following information, otherwise onlyoffice will treat it as processing and display the save error on the interface

{
    "error": 0
}

The callback function needs to save the latest file to the file server in the background. You can't only save the file path. onlyoffice is not responsible for storing the file

Reference documents

Posted by jrinco11 on Mon, 06 Dec 2021 14:49:36 -0800