Skip to content
Advertisement

How to send file from Mayan EDMS to external api?

I’m still learning Django and there is still a lot of unknown to me.

The problem is that I can’t pull the .pdf (or any other format) to be sent by ajax post method to external API. So on the reciving side I only get the string location of the file not the actual file.

I have put the following javascript code in the generic_list_items_subtemplate.html

JavaScript

On the reciving side I am getting

JavaScript

But I want to get the file not this string. I have no idea how to target file to send it dirrectly.

enter image description here

Advertisement

Answer

The URL “/documents/12/preview/” is the URL to view the document using the user interface. If what you want is the actual file of the document, you need to use Mayan’s API. Since documents in Mayan are a collection of versions, you need the ID of the latest version of the document you want. For this the API URL for the document detail which is:

“/api/documents/271/”

This will give you something like this:

enter image description here

From there look up the dictionary key “latest_version” and then the key “download_url”. The download URL in the screenshot is “http://127.0.0.1:8000/api/documents/271/versions/267/download/“. Do a GET request to this URL and you get the actual file of the document. Save the file in temporary variable (or Javascript Blob file object https://developer.mozilla.org/en-US/docs/Web/API/Blob) and then send it to your custom API with a POST request.

Another option is to just send the “download_url” to your API and have a worker process fetch the document data, that way you don’t have to store the binary data of the document in the browsers memory.

For more information about Mayan’s API go to “Tools” -> “API Documentation” and you’ll get an API documentation view that allows testing the API with the live data in your installation.

enter image description here

Advertisement