Downloading file using Spring Portlet MVC

1.2k Views Asked by At

I want to allow the user to save file using mvc portlet. In my code i am making an ajax call hence it has to be a resource response.

response.setContentType( "application/octet-stream" );
response.setProperty("Content-Disposition","attachment; filename=\""+fname+"\"");
response.setContentLength(b.length);
OutputStream  po= response.getPortletOutputStream();
po.write(b,0,b.length);
po.flush();
po.close();

In ajax response when i do alert(resp), I am getting the whole content of my file in alert but still not getting any option for download.

Please help and thanks in advance ;)

1

There are 1 best solutions below

1
On BEST ANSWER

Well, since you get the response, you need to allow the user to download it. You can for example use the HTML data: protocol and redirect the browser to smt. like

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyAA...

for a PNG image.

Anyway, can't you use a normal request to a portlet? It's trivial then.