We help IT Professionals succeed at work.

How to add "Preview file in IE" function?

programming02
on
261 Views
Last Modified: 2006-11-17
I'm buiding up a simple HTML editor.
I would like to add in a "Preview file Internet Explorer" function.
so that, when user click on the Preview button, the file can be previewed in IE.
How can I do this function?
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2016

Commented:
This is better on second thoughts. Use it as an inner class in your app. Then create your button thus:

JButton bnOpenInBrowser = new JButton(new BrowseAction());

SNIP==============================================


class BrowseAction extends AbstractAction {

      public void actionPerformed(ActionEvent e) {
            String fileName = getFileName(); // file name has to be got from somewhere
            new Thread(new BrowserOpener(fileName)).start(); // start browser on new thread
            
      }
      
      class BrowserOpener implements Runnable {
            String fileName;
            final String BROWSER_LOCATION = "C:/Program Files/Internet Explorer/IEXPLORE.EXE";
            
            public BrowserOpener(String fileName) {
                  this.fileName = fileName;
            }
            
            public void run() {
                  try {
                        Runtime.getRuntime().exec(BROWSER_LOCATION + " " + fileName)
                  }
                  catch(Exception e) {
                        e.printStackTrace();
                  }
            }
      }

}

Top Expert 2006

Commented:
use this command: << rundll32 url.dll,FileProtocolHandler C:\MyFolder\MyFile.txt >>

command = "rundll32 url.dll,FileProtocolHandler \"" + fileUrl + "\"";

this command opens the default browser, so it's not always going to open IE.
however it does not require you to know the full installation path of IE.

If you absolutely want IE to open, you could use the "ProgramFiles" enrivoment variable:
command = "\"%ProgramFiles%\internet explorer\iexplore\" \"" + filePath + "\"";

Note that you need to quote the paths that contain spaces.
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>use this command..

Not sure if that will work in all versions of Windows

Better to use the %PROGRAMFILES% variable, yes. I didn't know there was one ;-)
Top Expert 2006

Commented:
>>  rundll32 url.dll,FileProtocolHandler C:\MyFolder\MyFile.txt
This should work with all 32bit windows.
Will it change to rundll64 in the 64bit windows???

Anyway, using %ProgramFiles%\internet explorer\iexplore.exe
Should be fine in most cases since IE is part of windows and is installed in that directory by default.
But i don't know if that variable is available on all windows versions.

I guess it should be possible to have IE installed in a different directory, no?
I know that you can install 2 different versions of IE on the same windows if you install them in different directories.
But does that mean that you can get rid of the first installation.
You probably can if you modify the registry youself.

So, to be on the safe side, you should allow the user modify that command.
You can put the command string in a properties file for example.
We discussed that issue in another thread about how to open the default mail client, but i cannot find it anymore.
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>Will it change to rundll64 in the 64bit windows???

Maybe ;-)

>>I guess it should be possible to have IE installed in a different directory, no?

I think so, but as you say, that should be very rare.

>>But i don't know if that variable is available on all windows versions.

No, that's true. It may be better to have what i put at first ;-)

>>I know that you can install 2 different versions of IE on the same windows if you install them in different directories.

You sure about that?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
try the following:

  public static void launch(URL page)
  {
    try
    {
      String cmd = "cmd /C start " + url;
      Runtime.getRuntime().exec(cmd);
    }
    catch (IOException ex)
    {
       System.out.println(ex);
    }
  }
CERTIFIED EXPERT
Top Expert 2016

Commented:
That won't work < Winnt AFAIK
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Proof please.
Though you've actually suggested it previously :-D

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
You're probably not going to find a single solution that works on all versions of Windows (though I don't actually see this requirement in the question anyway) but  start... and rundll32 should cover the vast majority. So if one doesn't work, then try the other
CERTIFIED EXPERT
Top Expert 2016

Commented:
How about this?:

there's no program called 'cmd.exe' in < Winnt. Nor (AFAIK) is there one called 'start'
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
AFAIK != proof, especially considering that you yourself have suggested it before.
CERTIFIED EXPERT
Top Expert 2016

Commented:
It will certainly fail in < Winnt because of the first part. You might not realise this, but I'm not actually infallible, so the fact i suggested it before doesn't mean it's bulletproof ;-)
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
My above suggestion should cover most cases.
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>You're probably not going to find a single solution that works on all versions of Windows

You should be able to if you're prepared to use a library to access the registry
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Would seem a lot of effort to handle the very small percentage of case that are not handled by start or rundll32.
CERTIFIED EXPERT
Top Expert 2016

Commented:
'start' is OK < Winnt seemingly. The problem is the necessity of detecting the Win version because of the differing command interpreter
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> 'start' is OK < Winnt seemingly.

Perhaps in future you'll do your research before being critical of other comments.
CERTIFIED EXPERT
Top Expert 2016

Commented:
So far it's still the worst solution so far...
Top Expert 2006

Commented:
Aside the compatibility issues with some Windows version.
The main difference here is what the command does:

1. Open with IE:
>   "%ProgramFiles%\internet explorer\iexplore" C:\MyFolder\MyFile.xxx

2. Open with default browser:
>   rundll32 url.dll,FileProtocolHandler C:\MyFolder\MyFile.xxx

3. Open with default viewer:
>   cmd /C start C:\MyFolder\MyFile.xxx

It's up to you to choose which one you want to use.
From the most specific to the least specific.

You would choose solution 1 or 2 if you want to be able to open a ".txt" file in the browser for example.
Because there's a slim chance that the file association for ".txt" files would be configured to open IE.
You would choose solution 1 if you want to open an html file that uses VBScript or other IE specific code/plugin.
You would choose solution 3 if you want to open any document in the default viewer/editor.
Like opening a ".doc" or ".rtf" file in word or wordpad.
CERTIFIED EXPERT
Top Expert 2016

Commented:
Yes, with the proviso that of course one can't ignore the Windows version issues unless broken code is not an issue
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.