This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Problems downloading a file through Ajax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Problems downloading a file through Ajax" Watch "Problems downloading a file through Ajax" New topic
Author

Problems downloading a file through Ajax

Dave Paine
Greenhorn

Joined: Jan 09, 2009
Posts: 7
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



on my web browser, the file gets downloaded to the client machine. However, if I call the same code via some JavaScript, the code I call runs without any problems but the file does not get written. This is the relevant portion of the servlet



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:


The log output is exactly the same if I call the doGet (or doPost) from Javascript. Just that the file does not get written to disk (I am using linux and issued a find as root across the entire file system in case the file was downloaded but written somewhere else).

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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61299
    
  66

I'm confused about why you are confused. In the following code you do absolutely nothing with the response:

So why would you expect something to happen?

Additionally, what are you planning to do with the binary data once you have it in the script? There's not much that can be done with it.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Dave Paine
Greenhorn

Joined: Jan 09, 2009
Posts: 7
Hi Bear

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?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61299
    
  66

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?
Dave Paine
Greenhorn

Joined: Jan 09, 2009
Posts: 7
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61299
    
  66

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.
Dave Paine
Greenhorn

Joined: Jan 09, 2009
Posts: 7
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
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61299
    
  66

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problems downloading a file through Ajax