• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

How do I use java (client side) to download a file (PDF or jpeg) to my device (laptop)

 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to download a file that is stored in a database. The file may be an image or a PDF (at this stage, may expand to more formats later). The code I am using has no errors in the console log and returns success to the html/jQuery that calls it. However, the file is not downloaded. I have taken the image string, produced by "resourceDetail.getResourseImage()", and pasted it into codebeautify.org/base64-to-image-converter and it was displayed correctly. My code on the client side is:

                     

Kind regards,

Glyn
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you used the browsers developer tools (usually F12) to see what the response is from the request (usually found in the network tab)?
That includes the headers etc.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, hang on.

That's client side code?
Why are you setting up response stuff on the client side?

The client makes a request and receives a response, it doesn't build a response.
 
Saloon Keeper
Posts: 7413
170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This looks like HTTP stuff, but you mention that the file data is in a DB? DBs don't accept HTTP connections, so you'd need some sort of HTTP server in between that fetches the file's data from the DB, and then serve it to the client. Do you have that in place?
 
Rancher
Posts: 144
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the filename header looks wrong
it says "filename" - so in your code that's what you construct in outputFile
if this code is some sort of server code (maybe a servlet) there's no sense to give a full path as a filename
so - maybe it's the browser rejecting the illegal response from the server

overall - you should use a lib to handle http
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the OP:

"My code on the client side is: "

That's client side code.
 
Saloon Keeper
Posts: 26039
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:According to the OP:

"My code on the client side is: "

That's client side code.



That's the problem, however. It's alleged to be client-side code, but it's written like it's server-side code. It's all messed up.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quite.
Until we have a bit more clarity from the OP it's all going to be guesswork.
 
Glyndwr Bartlett
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Dave
1) Yes, I used the F12 however, only the console log to look for errors. I was not aware of looking in the network tab.
2) This is where I was advised to put it in a servlet.
3) Yes.

@Tim
1) Yes, I have made a call to the DB on the serverside and returned the details. The "resourceDetail.getResourseImage()" contains the image data pakage. I have pasted it into codebeautify.org/base64-to-image-converter and it was displayed correctly so the data is correct.
2) Yes, I agree that it is all messed up.

@Kristina Thank you :-)

After a month of working on this (I am not a programmer and am doing this self taught for my Scouts) I have found the answer. I started with a jQuery approach which worked in Chrome and not in IE. I was was told that it was not possible to do this in jQuery and be compatible and that I had to write a servlet. This was my attempt. I have now found: http://danml.com/download.html which works in Chrome and IE.

Thank you everyone for your input.

Kind regards,

Glyn
 
Make yourself as serene as a flower, as a tree. And on wednesdays, as serene as this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic