We help IT Professionals succeed at work.

Download file from server to client where client UI is EXT-GWT and framework is Spring

TOWELLR
TOWELLR asked
on
I am trying to download a file from the server to the client. My UI is EXT-GWT and the framework used is Spring framework. The file type is pdf. I need to file client to be able to save the file at any desired location....meaning I need the windows explorer box to pop up on the client side. Please advise.
Currently I am using the request builder and building the url to go to the controller and access the file and putting it in the response output stream. But it has not worked so far. any suggestions are welcome.
Comment
Watch Question

Kent WSr. Network / Systems Admin

Commented:
What to do with a file is set in the browsers mime and personal preferences.  I don't think you will be able to force clients to "choose" a location if they have told their browser to "always save to the /downloads folder".
The only way to override I know of is to hold shift while you click the link, forcing a file open save as...dialog box.

Commented:
I think if you set "content-disposition: attachment; filename=myfile.pdf" in the headers, browsers will try to save the file as myfile.pdf.
good luck,
Tim
Commented:
The parameter 'filelink' is the name of the file to be saved on the client side.

String fileLink = request.getParameter("filelink");
String pdfFile = "C:\\"+fileLink+".pdf";
File file = new File(pdfFile);
int length = 0;
byte[] bbuf = new byte[(int)file.length()];
ServletOutputStream op = response.getOutputStream();
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\""+fileLink+".pdf\"");
DataInputStream in = new DataInputStream(new FileInputStream(pdfFile));

while ((in != null) && ((length = in.read(bbuf)) != -1))
{
    op.write(bbuf,0,length);
}

in.close();
op.flush();
op.close();
file.delete();

The above code opens an explorer window so that the user can store the file at the desired location.

Author

Commented:
Tested the solution and it works fine.