Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems downloading a file through Ajax

 
Dave Paine
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dave Paine
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic