I have a server using Java's Spring library that generates a .csv file based on the parameter queryParam when I do an AJAX post on the client side.
$.ajax(
{
url: myUrl,
type: 'POST',
contentType: 'application/json',
data: queryParam,
success: function(response)
{
console.log(response);
},
error: function(jqXHR, textStatus, errorThrown)
{
console.error("error in query()");
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
}
});
The server-side code attaches the appropriate headers and writes the data directly to the response stream (please note I have to do it that way, since I cannot make a file on the server):
@RequestMapping(value = "/query", method = RequestMethod.POST)
public void exportSearchEvents(HttpServletResponse resp, @RequestBody QueryParams params)
{
Writer os=null;
try
{
resp.setHeader("Content-Disposition", "attachment; filename=\"searchresults.csv\"");
resp.addHeader("Content-Type", "application/force-download");
resp.addHeader("Content-Type", "application/csv");
resp.addHeader("Content-Type", "application/x-download");
resp.setContentLength(580);
resp.addHeader("Pragma", "public");
resp.addHeader("Expires", "0");
resp.addHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
os = resp.getWriter();
createFile(os, params); //writes the .csv data to the response PrintWriter object
os.flush();
os.close();
}
catch(Exception e)
{
logger.info("Error creating .csv file. " + e.getMessage());
logger.error("Error creating .csv file.", e);
}
logger.info("got to here. " + resp.toString());
if(os==null)
{
logger.info("Error creating .csv file: null");
logger.error("Error creating .csv file: null");
return;
}
return;
}
When it returns, I get a string back of the results (that should be in the .csv), but the download option box doesn't pop up.
Also, I should mention, I cannot create the file on the server and link to it (i.e. document.location.href = /server/createdfile.csv or whatever) from the browser due to file permissions that cannot be changed. And I have also tried various forms of hidden iframes that href to various places, but since I have to somehow give queryParam to the server, I can't do that (correct me if I'm wrong).
And because of limitations with Spring, simple POSTs and GETs won't work (I'm assuming it's an issue with Spring):
$.get(myUrl, queryParam, function(response)
{
console.log(response);
});
and
$.post(myUrl, queryParam, function(response)
{
console.log(response);
});
give me:
The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().
Also note that I'm avoiding Flash, so please, no answers regarding Flash.
Thanks in advance.