Download a file by using a REST endpoint

3
Hi there, I was wondering if it is possible to download a file (different formats) by using native Mendix REST. This works fine when using pdf's or images (the browser recognizes these files). But when trying to do this with excel or word files, this does not work.  We exposed an endpoint that returns a FileDocument but it doesn't recognize file format like .docx and .xslx (the file gets downloaded as a .zip).  Is there a way to solve this? Is it possible to return the file in a HTTP Response object? If so, what headers should we use? Thanks for your help!  
asked
2 answers
3

You can return those files, but you need to make sure that you return the correct headers, especially Content-Type

For .docx, set Content-Type to generatorapplication/vnd.openxmlformats-officedocument.wordprocessingml.document

For .xlsx, set Content-Type to application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

For an overview of Microsoft's mime types, see https://docs.microsoft.com/en-us/previous-versions/office/office-2007-resource-kit/ee309278(v=office.12)

Just for clarification, to add a header, add a 'Create Object' action to your microflow and choose System.HttpHeader. Set the HttpHeaders association to the HttpResponse, and set the Key and Value properties. (Key is the name of the header, so in this case 'Content-Type')

You can change the way the browser handles the file (as a download or viewing inside the browser) with the Content-Disposition header

answered
1

Hi Joost. Thanks for your answer!

How do you translate the filedocument to the "Content" field of the HTTP Response? This should be a string. Do you use the CommunityCommons Java-action StringFromFile? Or should I do this differently?

I've set the following headers:

  • Content-Type →application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8
  • Content-Disposition →attachment; filename = wordfile.docx

 

answered