How to set file name and/or extension in REST with Complex Obs?


I’m trying to create/read a Complex Data Obs from JavaScript and I was wondering if there’s a way to set the file name for the value that is returned as the obs.value.links.uri. I am using a Complex Data Obs with a BinaryDataHandler. My use case is as follows:

  1. Upload a PDF (e.g. somedoc.pdf) as a Complex Data Obs (works great!)
  2. Call Get on that Obs using REST (works great!)
  3. Read the uri property, which basically points to the value sub property of the Obs, e.g. http://localhost/openmrs/ws/rest/v1/obs/0f8b8084-cfc7-46d9-bb24-ecb40ef28e15/value
  4. Put that url in an tag
  5. When the user clicks on the link, it download the file, but the file is named file_01.file It would be better if the file was named file_01.pdf or even better somedoc.pdf.

It looks weird when the file downloaded is called file_xx.file, and I would like to avoid that if possible. Any suggestions?

I’ve attached a copy of the Requests and Responses that I am making.requests_and_responses.txt (2.3 KB)



Hi @jcelano,

Are you in the realm of the Ref App distro? If yes, could this module be of any use to you: Visit Documents UI (VDUI). It supports PDF files out of the box. I know that I’m not answering your question though. The developers behind the REST API for complex obs (@raff, @gutkowski) will most certainly do so, … if there’s a way, out of the current REST implementation, to set the file name upon downloading.

However, if your question is purely about JavaScript, you could wrap the REST calls and then generate a download link exactly the way you want it (see this for example). So: (1) You obtain the blob via REST then (2) you generate an hyperlink to the blob. This is an example of how it could be done in Angular 1.x:

var downloadLink = angular.element('<a></a>');
downloadLink.attr('href', $window.URL.createObjectURL(blob));
downloadLink.attr('download', obs.fileName);

What you’re interested in is the setting of the 'download' attribute.

@mksd, Thanks so much! This worked like a charm.:tada: And thanks for the link to the Visits Documents UI…I had looked at that, but only needed a very small part of the functionalty, so I decided not to use it.


@jcelano, thanks for bringing this up on talk. It’s a bug in REST, which can be easily fixed. Created an issue at