View Full Version : Open Files

02-28-2003, 03:28 PM
Hello all,

I have 2 problems, listed bellow:

1-) I'd like to use a open file dialog in my program, but I don't know the code in JavaScript that get me this function.
2-) I have an <INPUT type=FILE> in my page and I tried to set the value using the property value of this object of many ways: by JavaScript (document.form1.file1.value = 'xxxx') and manually, but nothing works, have anyone experienced this situation before! What should I do?

Thanks any help.

Roy Sinclair
02-28-2003, 03:59 PM
You don't set the value of a <input type="file"> tag. You can only get the value as set by the client, if your script could set the value websites could easily get any file they wanted from the client's computer, often without the user even knowing they've been robbed. It's a security thing, the browser will never let you set the value.

02-28-2003, 06:18 PM

Thanks for your reply, and for the 1st. question, do you have any trip???


Roy Sinclair
02-28-2003, 08:33 PM
Actually my response was to part 2 of your question, the <input type="file" /> tag you were using is the answer for part 1 of your question. The key thing to know is that you cannot script that form field for the user, they'll have to use that input field to navigate to the file themselves. That usually means some rather detailed instructions depending on the level of the users.

02-28-2003, 08:35 PM
It's much better to open files with a server-side language but if you must have a client-side solution here is an (IE) ActiveX object. Due to security, you will receive a prompt.. only by clicking yes will the file be loaded.

<script type="text/javascript">
function readFile(f)
var fso = new ActiveXObject('Scripting.FileSystemObject');
var fs = fso.OpenTextFile(f);
document.formName.fileContent.value = fs.ReadAll();
<form name="formName">
<br />
<textarea name="fileContent" rows="20" cols="100%"></textarea>
<br />
<input type="file" size="40" onchange="readFile(this.value)" />