how to download a csv file when clicking on a button widget

1
0
-1

Hi,

I'm trying to send some data in csv format from an application page when the user press a button.

I have a rest API method which is called when pressing the button. The code formats some data and should send them through the browser so that the user can download it.

I'm stuck when trying to send back the data, how should I format the response?

I get the following error right now :

AVERTISSEMENT: org.restlet.Component.LogFilter Addition of the standard header "Content-Disposition" is not allowed. Please use the equivalent property in the Restlet API.

Here what I have so far :

@Override
RestApiResponse doHandle(HttpServletRequest request, RestApiResponseBuilder responseBuilder, RestAPIContext context) {
try {
logger.info("Report");
String contenuCsv = "L1C1;L1C2\n\rL2C1;L2C2";

Map addHeaders = new HashMap();
addHeaders.put("Content-Disposition", "attachment; filename=DisplayName-file.csv");
RestApiResponse rar = new RestApiResponse( contenuCsv, HttpServletResponse.SC_OK, addHeaders, new ArrayList(), "text/csv", RestApiResponse.DEFAULT_CHARACTER_SET );
return rar;

} catch(Exception e) {
logger.log(Level.SEVERE, "Unexpected error during execution", e);
return buildResponse(responseBuilder, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Unexpected error during execution");
}
}

1 answer

1
+1
-1
This one is the BEST answer!

Finally found a solution.

First, in the UI designer, use a link instead of a button!

for the java code, use something like :

String fname = "report.csv";
Serializable conts = "elem1;elem2\r\nelem3;elem4";

responseBuilder.withAdditionalHeader("Content-Disposition", "attachment; filename="+ fname );
responseBuilder.withMediaType("application/octet-stream");
responseBuilder.withResponseStatus( HttpServletResponse.SC_OK );
responseBuilder.withResponse( conts );

return responseBuilder.build();

Notifications