admin管理员组

文章数量:1335136

In my application I want to open some files with the correct default programmes, like .doc file should be open with WORD and .psd files should be opened with Photoshop if it is installed, and this should be done under html or java script.

Please tell me how to do it.

In my application I want to open some files with the correct default programmes, like .doc file should be open with WORD and .psd files should be opened with Photoshop if it is installed, and this should be done under html or java script.

Please tell me how to do it.

Share Improve this question edited Sep 7, 2009 at 6:56 Jonathan Leffler 755k145 gold badges949 silver badges1.3k bronze badges asked Sep 7, 2009 at 6:34 AbhishekAbhishek 9973 gold badges21 silver badges44 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 1

There is no way for you to choose which application will be used to open your files with javascript...It just doesn't have that power.

Browsers typically don't have access to the puter's filesystem for security reasons. If you know the exact path to a file you can point the browser at it using a file: URI, e.g.

file:///C:/path/to/file.ext

You may also be able to do this with a plugin, eg ActiveX, however I am unsure as to what security measures that would have.

JavaScript cannot run programs, but if you have a file on your server you can simply link to it:

<a href='image.psd'>Download File</a>

Users will be promped to download the file or open it using the default program (for most files). Again - as others have said - this is determined by the browser. IE can open doc files on the browsers, and PDF documents can be opened that way too.

I don't think this is possible in JavaScript without using any activeX or something like that. Js has no access to locally installed applications.

Invoke the system mand 'open'. Works on Windows and Unix based clients.

Depending on where your script runs, you might not be able to invoke system mands though, for instance in a browser sandbox.

If you provide a link to a file on the local file system (eg: <a href="file:///C:/mydoc.doc">) then the browser will open it - however this is not a great way to do it since the browser will first show a dialog ("Do you wish to Save or Open") and then it will "download" it into temporary files as it would if the file were remote. In this case, if you edit and save the file, it'll be the version now in your temp folder. This might not be a problem if your files are read-only, but generally it's not a great user experience.

The only other method is to use ActiveX, which is actually rather easy (though I don't have the exact code on me now - write a ment if you're interested in a snippet and I'll update). Of course this es with the giant flashing caveats of:

  1. It only works in Internet Explorer.
  2. You need the user to fiddle with their security settings for the ActiveX scripts to run.

本文标签: javascriptopen a file with its default programmeStack Overflow