Here is my code to try to download a file using Vaadin. The button function is added within a component.
Button button = new Button("Generate");
@Override
public void buttonClick(ClickEvent event) {
try{
VaadinResponse resp = VaadinService.getCurrentResponse();
HttpServletResponse response = (HttpServletResponse) resp;
response.setContentType("text/csv");
response.setHeader("Content-Disposition", "attachment; filename=\"Report.csv\"");
OutputStream outputStream = response.getOutputStream();
String outputResult = "Id,name,dept,salary\n";
outputStream.write(outputResult.getBytes());
outputStream.flush();
outputStream.close();
}catch(Exception e){
e.printStackTrace();
}
But I get the following error Invalid JSON response from server:Id,name,dept,salary
Please help in identifying where I have gone wrong.
Thanks
You can't implement a file download like that. The Vaadin client is expecting a specifically formatted response. You can use a FileDownloader extension instead: https://vaadin.com/docs/v8/framework/articles/LettingTheUserDownloadAFile