Hi. I am trying to have my servlet write a file to the client machine and have come across a strange problem. If I enter
I have doGet (and doPost) set up to simply call this function with the relevant parameters.
The log file if I call this servlet directly from the browser is:
Here is the code I am using to make the call
Can anyone shed some light on this? I get the same result on Windows and Linux using Firefox and Chrome. As an aside - maybe related - I thought that setting the response header to have a Content-Disposition of attachment would prompt the browser to ask the user where to place the file, but it doesn't do that no matter how I invoke doDownload.
Checking the status codes is secondary. By the time I get to that piece of code the file should have been written to the client file system by the servlet, but it hasn't been. I am not particularly worried about what happens when the script regains control - I plan to just output a success message. But The servlet should write the file before the script is called - or are you suggesting it is the job of the script to write the file?
Dave Paine wrote:Checking the status codes is secondary. By the time I get to that piece of code the file should have been written to the client file system by the servlet
Ummm, no. The servlet has no access whatsoever to the client file system. So, no.
All the servlet does -- and all it can do -- is to stream the content of the file as the response.
But The servlet should write the file before the script is called
See above. No. How do you figure it can do that?
or are you suggesting it is the job of the script to write the file?
I'm not suggesting that at all because it can't. Script has no access to the client file system for security reasons.
So my question remains: why are you using Ajax at all for this when it's not going to be able to do anything you want with the returned data?
Joined: Jan 09, 2009
I think I am beginning to see. What I am trying to do is allow the user to receive a file downloaded on their computer when they click a button. I thought that the servlet would do this since if I access the servlet directly from the browser the file gets written locally. But indeed, I had read that the servlet cannot access the file system for security reasons. All it can do is send the file back in the response, which I think it is doing. So I guess my question should be - how can I access that response and write that file to disk? If Ajax can't access the file system and thus cannot do what I need, what should I use? Thanks for your time, Bear.
It's clear that Ajax is not the right tool for this particular job. I'm still not sure why you went that route to begin with.
You need to submit the download request as a normal GET from the browser as a link or form submission. That will result in either the response being displayed in the browser or a Save As dialog so that the user can download the file. Given that you've set the content disposition as an attachment, the latter should occur. But sometimes it's completely up to how the browser is configured.
Joined: Jan 09, 2009
Bear - thank you for that pointer. I'm writing so many Ajax routines these days that I just got tunnel vision. Using a simple form was all that was needed - so simple - (when you tell me). Thanks for that