Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 613
  • Last Modified:

Opening File in viewer outside browser

I have a JSP page where I have a hyperlink to view a file. The file could be a word doc, or a pdf doc. If the user clicks on the link the document is opened in the browser. Is there a way to open it outside the browser by launching the corresponding viewer. The browser view should remain the same as it was befoore the link was clicked.

here is the simple JSP (only posting the relevant code)

   <a href="Popup">Click here to download</a>

The servlet that writes the file


public class Popup extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException   {

            String fileName ="XMLAppuserguide.pdf";
        response.setContentType("application/pdf");
       
            ServletOutputStream sos = null;
        sos = response.getOutputStream();
       
            File file = new File(fileName);
            System.out.println("-----------------"+file.getAbsolutePath());
            FileInputStream fis = new FileInputStream(file);

       
            int  b = fis.read();
        while (b!=-1) {
           sos.write(b);
           b=fis.read();
        }
    }
}

0
thomas908
Asked:
thomas908
  • 5
  • 3
  • 2
  • +3
4 Solutions
 
Ajay-SinghCommented:
<a href="1.exe">Click here to download</a>
 
should work
0
 
thomas908Author Commented:
>><a href="1.exe">Click here to download</a>
I need to do it through a servet, since there is some other logic in servlet that I have not posted.
0
 
ZvonkoSystems architectCommented:
Like this:

<a href="Popup" target="_blank" >Click here to download</a>


0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ZvonkoSystems architectCommented:
And if you want to open and reuse one single popup window on every link klick, then give the popup window a Name:

<a href="Popup" target="myPop" >Click here to download</a>

0
 
thomas908Author Commented:
<<<a href="Popup" target="_blank" >Click here to download</a>

But this still opens it in the browser. I want the application to be launched directly.
0
 
thomas908Author Commented:
To be more precise with what I posted above, there'll be only .tiff images that need to be opened in the microsoft image viewer. There are no pdf and msword files, I posted that just as an example.
0
 
thomas908Author Commented:
When I give the path to .tif image in my servlet, then I get an open/save dialog box. Is there a way to compress this dialog box, so that the image directly opens in the viewer.
0
 
Michel PlungjanIT ExpertCommented:
try
response.setContentType("application/octet-stream");
response.setContentDisposition("attachment; filename=\""+filename+"\"");
0
 
thomas908Author Commented:
Getting this error

Abc.java:18: cannot resolve symbol
symbol  : method setContentDisposition (java.lang.String)
location: interface javax.servlet.http.HttpServletResponse
        response.setContentDisposition("attachment; filename=\""+file.getName()+
"\"");
                ^
1 error
0
 
Michel PlungjanIT ExpertCommented:
response.setHeader("Content-disposition","attachment; filename=\""+file.getName()+"\"");
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> I need to do it through a servet, since there is some other logic in servlet that I have not posted.

Try a download servlet like this:

http://jspwiki.org/wiki/MakingADownloadServlet
0
 
KuldeepchaturvediCommented:
well as everybody above has said.. you will have to set the contnet type of your response data depending upon what kind of data you are sending to the client.

for example for a MS-word file you will have to do

response.setContentType("application/msword");
for a gif image it has to be..

response.setContentType("image/gif");

and so on..

you will have to cover a lot of mime types to have it all covered.
0
 
ZvonkoSystems architectCommented:
Did you try:

            String fileName ="myImage.tif";
        response.setContentType("image/tiff");


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now