469,267 Members | 922 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,267 developers. It's quick & easy.

File Directory Dialog Box

Is there a way in Javascript to bring up dialog boxes for both opening and
then also saving a file?

Jul 23 '05 #1
12 35157
In article
<96******************************@localhost.talkab outprogramming.com>,
rp*****@nospam.indy.rr.com enlightened us with...
Is there a way in Javascript to bring up dialog boxes for both opening and
then also saving a file?


Please clarify the question.
What file is being opened?
What do you mean by 'dialog box'? A prompt? A window? A "browse" button? A
modal dialog window?

What do you mean by "and then also"? Same button/prompt/whatever does both
tasks? A dialog box for both tasks, but the dialog box is not the same
object?
--
--
~kaeli~
Is it true that cannibals don't eat clowns because they
taste funny?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #2
This is going to be a file compare program. I just need to have the
standard windows open file dialog box open so the user can select the
file, i will use this twice so they can choose the two file's to be
compared. After the compare i want the standard windows save as box to
appear so they can save the results file.

Jul 23 '05 #3
> I just need to have the
standard windows open file dialog box open so the user can select the
file, I will use this twice so they can choose the two files to be
compared. After the compare I want the standard windows save as box to appear so they can save the results file.


In general, Javascript cannot read or write files from the local
filesystem.

You can create an "open" dialog by creating an <input type="file"> form
element and could probably open it by invoking its onclick method
(though I haven't tried it). But I don't think that the content of the
selected file is available to Javascript (again, I haven't tried).
Similarly a server-side implementation can trigger a "save as" and/or
"open with" dialog by returning content of a type that the browser
cannot handle natively, but I don't believe there is a way for
Javascript to generate such content.

I would be curious to know if anyone can suggest a way of doing this.

--Phil.

Jul 23 '05 #4
I dont plan on having the browser open and doing this in an html page, i
guess i was assuming you could run javascript by itself like vbscript

Jul 23 '05 #5
In article <4d7cc225a92f001ec7ced86507adebc5
@localhost.talkaboutprogramming.com>, rp*****@nospam.indy.rr.com enlightened
us with...
This is going to be a file compare program. I just need to have the
standard windows open file dialog box open so the user can select the
file, i will use this twice so they can choose the two file's to be
compared. After the compare i want the standard windows save as box to
appear so they can save the results file.


The file input type prompts for a file on button click.
<input type="file">

For saving the results, I believe this would work. (Windows / MSIE only?)
document.execCommand('SaveAs');

If this is running on a PC, you may want to look into using an HTA with
Windows Script Host. You don't tend to run into as many security problems
that way. You get more stuff to play with, too. Check out FileSystemObject.

HTH
--
--
~kaeli~
Abdicate (v.), to give up all hope of ever having a flat
stomach.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #6
You can.. as an HTA application.
HTA gets a lot of bad press because it's an obvious tool for virus
writers, but it effectively allows you to write an HTML application
that lives outside the Browser sandbox. If you're not looking to put
it online, and need a rapid dev environment, it's ideal. I've used it
to produce simple applications before that leverage the easy displaying
power of HTML, to be run from a local pc or over a network. Obviously
all your code is visible, so not recommended for anything that requires
security.

Also, because it's out of the sandbox, you can do all manner of loading
and saving files using the standard FileSystemObject (or any other Com
type stuff).

Jul 23 '05 #7
Where can i get information on HTA.

Jul 23 '05 #8
Kind of hard to find definitive guides to all it's uses, but
http://msdn.microsoft.com/workshop/a...taoverview.asp
gives you an overview. Basically it's an HTML file, saved as .HTA, run
using MSHTA.exe (in windows path).
Think it only really needs some sort of rudimentary tag in the head
like
<HTA:APPLICATION ID="oHTA"
APPLICATIONNAME="MyApp"

From there just build HTML as usual. I keep my scripts in a seperate

..js file, linked in.

It's sometimes hard to find Javascript syntax for things like
FileSystemObject, but the info is out there on the web. Takes some time
to find, and if you are familiar with the VB versions, it doesn't
require much altering.

You could of course write the HTA using VBScript...

Jul 23 '05 #9
I have coded the open dialog in vbscript but couldn't find the save as, as
vbscript is the preferred method for the client.

Jul 23 '05 #10
"rockocubs" <rp*****@nospam.indy.rr.com> writes:

(please remember to include *some* context for your posting)
I dont plan on having the browser open and doing this in an html page, i
guess i was assuming you could run javascript by itself like vbscript


You can. On Windows-boxes, you can use any language supported by the
Windows Scripting Host. E.g., make a file called "test.js" containing:

alert("this works!");

and run it with "wscript test.js".

However, Javascript itself has no method of working with files, or any
other I/O for that matter. All of that is provided by the environment
the script is run in. The environment in browsers is the one assumed
by people in this group unless something else is stated.

If you run your scripts using "wscript", you should consult the
documentation for the Windows Scripting Host to find out what features
are available.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #11
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:wt**********@hotpop.com...
"rockocubs" <rp*****@nospam.indy.rr.com> writes:

(please remember to include *some* context for your posting)
I dont plan on having the browser open and doing this in an html
page, i
guess i was assuming you could run javascript by itself like vbscript
You can. On Windows-boxes, you can use any language supported by the
Windows Scripting Host. E.g., make a file called "test.js" containing:

alert("this works!");

and run it with "wscript test.js".


And get the error:
Script: \path\test.js
Line: 1
Char: 1
Error: Object expected
Code: 800A138F
Source: Microsoft JScript runtime error
However, Javascript itself has no method of working with files, or any
other I/O for that matter. All of that is provided by the environment
the script is run in. The environment in browsers is the one assumed
by people in this group unless something else is stated.
The error encountered above is precisely because of this fact. alert()
is a method provided by the default global object to scripts executing
in Internet Explorer. alert() is not a method available to scripts
running in wscript/cscript because the host does not expose/provide such
a method.

To popup a dialog in wscript, you'd use:

WScript.Echo("this works!");

(note this echos to the console when the script is executed with cscript
test.js).

To have a version that does the equivlent of alert() for both wscript
and cscript, you'd use:

var wshShell = new ActiveXObject('WScript.Shell');
wshShell.Popup("this works!");
If you run your scripts using "wscript", you should consult the
documentation for the Windows Scripting Host to find out what features
are available.


Indeed.

For relatively clean list of available WSH objects, properties and
methods, I prefer <url:
http://www.devguru.com/Technologies/...h_objects.html />

All the objects available there are instatiated (with the exception of
TextStream, which is returned by the StdErr, StdIn and StdOut properties
of the WScript object) in JScript using:

var wshWhatever = new ActiveXObject('WScript.Whatever');

There are also all sorts of other ActiveXObjects which are available
from WSH, such as the FileSystemObject <url:
http://msdn.microsoft.com/library/en...FileSystem.asp
/>

Note that these references are in no way intended to be a complete list
of references, only a sample of the types of references available on the
Web. Other references can be obtained by using Google to search for
something like:

"<object you are interested in> activexobject site:microsoft.com"

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #12


*** Sent via Developersdex http://www.developersdex.com ***
Jul 23 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by bbxrider | last post: by
2 posts views Thread by yanivoliver | last post: by
reply views Thread by thomasp | last post: by
3 posts views Thread by Highlander | last post: by
11 posts views Thread by GaryDean | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.