I am currently attempting to have the browser automatically prompt the user to save a file from server. I have a Java Servlet coded as follows:
private void doDownload( HttpServletRequest request, HttpServletResponse response){
File f = new File(<filename.ext> //This is text file but I have tried with pdfs, gifs, zips
ServletOutputStream op = response.getOutputStream();
int length = 0;
op = response.getOutputStream();
String mimetype = context.getMimeType( f.getAbsolutePath() );
resp.setContentType("application/x-download");
response.setContentLength( (int)f.lengt开发者_开发技巧h() );
response.setHeader("Content-disposition", "attachment; filename=<newFileName.ext>");
byte[] bbuf = new byte[8192];
DataInputStream in = new DataInputStream(new FileInputStream(f));
while ((in != null) && ((length = in.read(bbuf)) != -1))
{
op.write(bbuf,0,length);
}
in.close();
op.flush();
op.close();
}
I am using Firefox to do the testing and I have Firebug running. I can see the Firebug response contains the headers as I have set them and that when the request is for a text file, all the text that should be there is contained in the response.
On the client I have Javascript making the request asynchronously as follows:
try{
xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
}
xmlhttp.open("post", 'myUrl?action=download', true);
xmlhttp.send(null);
I can see that the request makes contact with the server and that the response is correct, as I said above that I can see the text file etc. in the Firebug response output.
The problem is that nothing happens from here, there is simply no browser response. I have scoured the internet for the correct way to do this but all that I can find is that the server needs to set the content-disposition to "attachment; filename=" and that the contentType should be set to "application/x-download". I have tried setting the contentType to "application/octet-stream" but nothing that I have tried seems to work.
Please can someone explain to me if there is something that I am doing wrong?
Browsers do not display the "Save as" dialog when a XMLHttpRequest
object is used to retrieve a document from the server. This is quite simply because of the fact that the parsing and rendering of the response is controlled to a good degree by client-side JavaScript that processes the responseXML/responseText properties, instead of the browser's HTTP response parsers and MIME handlers.
If the browser were to automatically start handling the contents of these, then the purpose of the XMLHttpRequest object would be lost, and no partial page updates could be made to the browser's DOM (isn't that what everyone wishes to do with AJAX?).
Therefore, if you wish to have the browser present the "Save as" dialog, is to trigger an event that will cause a full page refresh that would result in a request being sent to the server, with the response being the document that ought to be displayed. This can be done via
- a form submit, or
- a click of a hypertext link (anchor elements), or
- creation of inline frames (iframes) within a page, with the source attribute set to the document to be displayed in the frame.
You have to make the browser "visit" the page for the download dialog to appear. Just sending a post request is not enough.
Probably the best way to do this with JavaScript is to dynamically make a <form>
object with action="/your/servlet/page"
and then use JavaScript to call submit()
on the form object (don't forget to add the <form>
to the page or it won't work).
精彩评论