I want to create and then download a simple csv file on the client side. When the user click on the open button in the view, it opens the csv file so the user can download it on his PC.
Here's my javascript code :
$("#booksList").on('click', '.export-excel' ,function() {
var json = { };
$.ajax({
type : 'POST',
data : JSON.stringify(json),
contentType: 'application/json; charset=utf-8',
url : '@routes.BooksController.exportExcel()',
success: function(data){
alert('Success');
},
error: function(data) {
alert('Failed to load csv file');
}
});
});
Here's my java code:
public static Result exportExcel() {
String filename = "outFile.csv";
FileWriter fileWriter = new FileWriter("c:/"+filename+"");
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write("This is test line 1.");
bufferedWriter.newLine();
bufferedWriter.write("This is test line 2.");
bufferedWriter.flush();
bufferedWriter.close();
return ok(new java.io.File("/tmp/"+filename));
}
My route :
POST /app/exportCSV controllers.BooksController.exportExcel()
But i get a 500 (Internal Server Error) with no details !
What i'm doing wrong ?