Skip to content

Commit

Permalink
Export multiple CSV files out to browser as a single zip (moqui#194)
Browse files Browse the repository at this point in the history
* Fix bug when setting content type

* Export multiple CSV files out to browser as a single zip

* Use 'application/zip' when sending a zip file
  • Loading branch information
akasiri authored Mar 31, 2022
1 parent fd864a5 commit 3e5ad8b
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions base-component/tools/screen/Tools/Entity/DataExport.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,23 @@ along with this software (see the LICENSE.md file). If not, see
} else if (context.output == "browser") {
// stream to ec.web.response
def response = ec.web.response
if (fileType == 'JSON') response.setContentType('application/json')
if (fileType == 'CSV') response.setContentType('text/csv')
else response.setContentType('text/xml')
response.setCharacterEncoding("UTF-8")
response.setHeader("Content-Disposition", "attachment; filename=\"EntityExport_${ec.l10n.format(ec.user.nowTimestamp, 'yyyyMMdd_HHmm')}.${fileType == 'JSON' ? 'json' : fileType == 'CSV' ? 'csv' : 'xml'}\";")
response.setHeader("Cache-Control", "no-cache, must-revalidate, private")
edw.writer(response.getWriter())
if (fileType == 'CSV' && entityNames instanceof List && entityNames.size() > 1) {
// if trying to export more than one entity as CSV, send a .zip file instead
response.setContentType('application/zip')
response.setHeader("Content-Disposition", "attachment; filename=\"EntityExport_${ec.l10n.format(ec.user.nowTimestamp, 'yyyyMMdd_HHmm')}.zip\";")
edw.zipDirectory('', response.getOutputStream())
} else {
if (fileType == 'JSON') response.setContentType('application/json')
else if (fileType == 'CSV') response.setContentType('text/csv')
else response.setContentType('text/xml')
response.setHeader("Content-Disposition", "attachment; filename=\"EntityExport_${ec.l10n.format(ec.user.nowTimestamp, 'yyyyMMdd_HHmm')}.${fileType == 'JSON' ? 'json' : fileType == 'CSV' ? 'csv' : 'xml'}\";")
edw.writer(response.getWriter())
}
noResponse = true
}
]]></script>
Expand Down

0 comments on commit 3e5ad8b

Please sign in to comment.