Hi, I’ve setup a community edition server and we are evaluating onlyoffice for a compliance service. An important workflow is that during an audit, and auditor may ask for copies of documents. We have a PHP script that currently finds them in our local file share and zips them up so the auditor can take them with them. I have to believe this is possible with onlyoffice, but i can’t for the life of me find an example or this specific concept in the documentation.
So my question is, is it possible through the API and a PHP script to download documents if we know the document ID?
Hello @RoadReady
Please take a look at this method that generates a download link to the specified document:
https://api.onlyoffice.com/portals/method/files/get/api/2.0/files/file/%7bfileid%7d/presigneduri
Note that this method requires authentication. More about it you can find here:
https://api.onlyoffice.com/portals/auth
Ok I finally got it working, your documentation is a bit ‘meta’ but I figured it out. I can get a link back that, yes, 100%, downloads the file. However, I need to be able to automate this and pull the file down in a CRON job. While the url endpoint seems to generate a payload fo a browser which inititaes a file download, it is not happening when I reference the link in PHP code using get_file_contents. I need to be able to download multiple documents automatically, not with a person clicking in a browser, so the provided answer does not fulfill the use case…
Hi I need to bump this thread. Is there an answer?
Sorry for the late response.
Unfortunately, we haven’t tested such scenarios with PHP scripts and I cannot provide any further ideas on what to do next. It is all up to you as a developer.
Possibly, you could find something more suitable for you here:
https://api.onlyoffice.com/portals/section/files/files
Jeez, ok. I’ll tell the 650 franchises we were going to put on onlyoffice that we need to find another tool. Thanks!
That is unfortunate to hear.
Just to make sure, are we talking about getting a document from Documents module on the portal?
Getting a document by FileID. We need to find a system exactly like OPENOFFICE, with the one addition that, on demand, we can have an automated process pull down files to provide to a non-user. These are auditors, we want to put all the docs somewhere, then when audited be able to provide the requested documents all packaged up in a ZIP file. The links you gave me to everything, except, they expect the link to be clinked in a browser. We need to be able to get a link that lets us programmatically download the file. The current link doesn’t return the file, it provides instructions for the browser to download the file. This isn’t about ‘my ability as a programmer’ as you so kindly suggested above; or the fact that you don’t test your API with non-Microsoft languages… We are evaluating your system for a major rollout…
Here is the way to download the file with given in this thread methods:
- First, we need to get the authentication token. For that you can use POST api/2.0/authentication method. After sending the request to API, you will receive such response:
{
"count": 1,
"status": 0,
"statusCode": 201,
"response": {
"token": "<your_token>",
"expires": "2024-05-30T12:47:54.7762210Z"
}
}
From this part we have to fetch the <your_token>
value which will be used for further request.
- Second, we have to get the download link for the specified file with GET api/2.0/files/file/{fileId}/presigneduri. From the response we fetch the link to the file marked as
<link>
:
{
"count": 183,
"status": 0,
"statusCode": 200,
"response": "<link>"
}
- After getting
<your_token>
and <link>
, you can use this to get the file on your machine with specified <document_name>
and <ext>
(extension), e.g. test.docx
:
curl -X GET -H "Authorization:<your_token>" "<link>" --output <document_name>.<ext>
Note that I’ve used POSTMAN to send the requests and to get the responses for this demonstration.
It Worked! Thank you. This was the very last piece we needed, all good.
1 Like
Hi @Constantine , I just wanted to know that what will be the return type of the 3rd step mentioned in your solution above. Will I get the Document Bytes of the document?
And also in the 2nd API, i.e. to get the “presigneduri” should “fileId” be passed as a path variable or as a query parameter? And at last, can you also help me by providing a sample of what the “file download link” will look like? Just a sample link will work.
I am using Java 8 and looking to integrate with Only Office by using your APIs/.
Hello @Devyansh
Method GET api/2.0/files/file/{fileId}/presigneduri returns a direct link to download the file. I’m not sure if I follow your definition Document Bytes, please elaborate on this.
In the body of the request you have to specify the ID of a file that you want to generate a download link for. For example:
{
"fileId": "<ID>"
}
ID can be found by hovering your mouse over a file in the Document module, in the botton-left corner of a browser window you’ll see a link with fileId=<ID>
on the end.
Here is the example of the whole response upon succesfull execution of the method:
{
"count": 1,
"status": 0,
"statusCode": 200,
"response": "https://<portal_address>/Products/Files/HttpHandlers/filehandler.ashx?action=stream&fileid=<ID>&version=1&stream_auth=427297688231.L45YPTDGYU2JQKKAANGZUWVT9WFOSFBLPFTCHFHCCC"
}
Note that I’ve used POSTMAN to send the requests and to get the responses for this demonstration.
Also note that these methods are working for Workspace portal only. They cannot be used for Document Server.
1 Like