web page file download issue

I’m running openmrs 2.3.0 Build b3ade0 on intel mac using “Descargar reporte

It works fine. I’m trying to run the same download on openmrs 2.4.3 using apple m4 and the file could not be found error. Any suggestions on how to get the download working on 2.4.3 would be appreciated. The file of interest is located in the server folder “reports”

Sorry, the link is <%= ui.resourceLink(‘/reports/individualServicesReport.csv’)%>" download=“individualServicesReport.csv”>Descargar reporte

This is likely due to how ui.resourceLink() works in newer versions.

In 2.4.3, it only resolves module/static resources—not files from server folders like /reports. That’s why you’re seeing “file not found”.

Fix:

  • move the file into your module’s resources folder and use ui.resourceLink("yourmodule", "..."), or

  • serve it via a controller endpoint and link to that.

The controller approach is more reliable.

Sorry, I wasn’t clear. The files I want to download are only known during runtime so they can’t be included when the module is compiled - they can’t be included in the “omod” file.

Got it, thanks for clarifying.

In this case, ui.resourceLink() will not work because it only serves static module resources, not files created at runtime.

For files in /reports, you should expose them through a controller and stream them as a download.

Example:

@RequestMapping("/reports/download")
public void downloadReport(@RequestParam("file") String fileName, HttpServletResponse response) throws IOException {

    if (fileName.contains("..")) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }

    File file = new File(OpenmrsUtil.getApplicationDataDirectory(), "reports/" + fileName);

    if (!file.exists()) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }

    response.setContentType("text/csv");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");

    Files.copy(file.toPath(), response.getOutputStream());
    response.flushBuffer();
}

Then link to:

/openmrs/reports/download?file=individualServicesReport.csv

This serves the file through the application, which works in newer OpenMRS versions.

Hey Barry – one idea I would start off with is to determine if this is an asset path or resource serving issue, not necessarily a file one. Since you are using: ui.resourceLink(‘/reports/individualServicesReport.csv’) I would double check that: Is ‘/reports’ still being served as a web accessible resource in version 2.4.3, or has there been any changes in resource resolution? Does the file work if you navigate to the created url directly in the browser? Is the file actually located in the modules/resources path that ui.resourceLink() expects, or is it just stored in a server side directory called ‘reports’? There could be some errors in the logs around permissions/path problems for the file, especially because the environment has also changed (Intel → Apple Silicon). It may help to debug what URL ui.resourceLink() creates in each version and compare them. It will be interesting to know if anyone else has run into resource serving issues between 2.3.x and 2.4.x releases.

  • ashthe25

Thanks much for your help. I just realized I’ll need to maintain the older version of OpenMRS, 2.3.0 so I’ll keep the old code that works for uploading/downloading files.