XP help creating files

Hello,

I have a few questions for you guys.
I have a page in my project that creates a csv from the data we have. Currently we’re using the data:application/octet-stream href with an html 5 download attribute, but that solution isn’t really crossbrowser (with ie and safari refusing to help).

What I want to know is, how can I use a service in enonic to output my file setting headers like content-type, content-disposition, filename and so on (like we would do if using java, php and so on)?

As a restriction for that project, we will be using enonic cloud, so we don’t have access to the kernel.

If we could create files in the server, that would work as well. But the documentation for the I/O libraries don’t mention anything of the sort.

Thanks!

1 Like

Your service can return a response object with any headers you like.

{
  "status": 200,
  "body": "Hello World",
  "contentType": "text/plain",
  "headers": {
      "key": "value"
  },
  "cookies": {},
  "redirect": "/another/page",
  "pageContributions": {},
  "postProcess": true,
  "applyFilters": true
}

For the file, you might want to check the documentation for the IO lib and the content.getAttachementStream().

2 Likes

It’s right what @mla says. You can return the required headers in a controller (or service). For your case, I believe it should return the following:

{
  "status": 200,
  "contentType": "text/csv",
  "body": myCSV,
  "headers": {
    "Content-Disposition": 'attachment; filename="myfile.csv"'
  }
}

Read more about the content-disposition header here: https://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html

1 Like

Thanks for the answers!
I’m still unable to see a way to create files using the IO lib, it’s just not there in the documentation, or at least it’s not that easy to understand. But I could manage just returning the headers from my service.

Thread can be closed, thanks!