views:

238

answers:

4

hello everyone I need to know if there is a way in java/servlet to make documents(doc,pdf) stored in database available for download to users in requested way(please see below),

for example there is a web page and the link for document in it

right now it is done this way: if the user clicks that link than a new blank window opens and there the download dialog box is shown and the user is able to download the document but that blank window stays open and the user have to close it manually

but wish to do it this way: If the User clicks that link than directly staying on that page a download dialog box should show up asking them to save the file

a servlet url handles the download of the document which is responsible for extracting the doc form database and makes available for download to users

thank you for your time and effort

+1  A: 

I wonder if your link html doesn't have something like:

<a href="/foo" **target="_blank"** ....>download</href>

Otherwise, it should work as you want.

Dave
thanks you but the link is not using the target attributebut yes similar it opens the page as another window like popupthe link trigers the javascript function which opens a page which handles the document download (servlet url)but if i remove the popup window that the current page will go blankbut is there a way that the current page stays what it is and just the file download box appears
fossil
A: 

You need to remove target="_blank" from your <a> element.

Edit: as per your comments: you need to set Content-Disposition header to attachment. You can find here examples of a simple fileservlet and an advanced fileservlet to get some insights.

BalusC
thanks you but the link is not using the target attributebut yes similar it opens the page as another window like popupthe link trigers the javascript function which opens a page which handles the document download (servlet url)but if i remove the popup window then the current page will go blankand i do not want thatIs there a way that the current page stays what it is and just the file download box appearsthanks
fossil
A: 

This is a bug in IE which depends on several things, the content type is one of them. We had the same problem a few years ago but I don't remember the correct solution anymore, only that we struggled with this for quite some time. Try this:

  • Use the correct content type (application/pdf)
  • If that doesn't work, use a wrong file type (like application/octet-stream) which should tell IE to leave the file alone. You may have problems with the file extension, though.
  • Send or don't send the correct file size
  • Check which chunking mode you're using.

One of these things made IE behave. Good luck.

Aaron Digulla
that sound good but it shows me the dialog window and asks the user to opne ost of the document are msword (.doc) but hte problem is I need to open the servlet url so that page extracts the document fomr database and make it available to download for the user but that blank page stays open after the user have downloadedand as suggested by the above helpers to not to open another window but that will make my current page the current servlet page whcih is blankthanks for you tips
fossil
If the browser opens a window, then it thinks that it can display the content. Use a different content type and play with the headers of the response as suggested by ZZ Coder.
Aaron Digulla
thanks i will try that
fossil
+1  A: 

You need to add following headers in your servlet to make it a downloadable content so browsers don't try to display it,

String value = "attachment;filename=\"" + URLEncoder.encode(filename, "UTF-8") +'"';
response.setHeader("Content-Disposition", value);
response.setHeader("Content-Transfer-Encoding", "binary");

The filename is proposed filename and user can change it.

ZZ Coder
so if we add this part and do not open a new page then will the browser only show the download dialog box and not the blank page which is the servlet page and stay on the current page?for example we are on Page "A"user clicks a link on page "A"no new window and the page "A" stays there as it is and only the download dialog window will show asking the user to download the document?thanks for you help
fossil
Yes. That's how we do download in our server. It works on most browsers.
ZZ Coder
BTW, I was just looking at our server. Someone commented out "Content-Transfer-Encoding" with no explanation. I guess some browsers may not like that header. You might want try without that.
ZZ Coder
great!, thanks i ill try that and see if that works
fossil
this is fixed I was opening a new window, think because of the pdf files but changed the "inline" to "attachment" and it works like a charmthanks for you help
fossil