Forum Moderators: open

Message Too Old, No Replies

How to access <Input type="file" name="myname[0] in Netscape

         

jrfurman

2:14 am on May 9, 2003 (gmt 0)

10+ Year Member



Hi,

I have a form in which I have multiple file input fields on it. The name attributes of the fields are in array form to simply the upload feature in php.

I access the fields via the id attribute in MSIE but have to access the fields via the name attribute in Netscape.

SO for example:

<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
changesrc() {
str = document.listor_form.Photofile[0].value;
str = str.replace(':','¦');
str = str.replace('\\','/');
str = str.replace(' ','%20');
document.listor_form.photo1.src = "file:///" +str;
}
</script>
...
...
<form name="listor_form" method="post" ENCTYPE="multipart/form-data">
<img name="photo1" src="default.png"
<input name = "Photofile[0]" type="file">
<input name = "Photofile[1]" type="file">
<input name="preview" type="button" value="Netscape Users Click Here to Preview Images" onClick="changesrc();">
</form>

NS doesn't seem to like the document.listor_form.Photofile[0].value

Also if any one can tell me how to shorten my str.replace code I'd appreciate it.

It really bites that in order for the onchange event for the <input type=file> only works when the focus of the field is lost.

Thanks for all the help.

jrfurman

2:16 am on May 9, 2003 (gmt 0)

10+ Year Member



I guess I should have referred to attribute as Property

Sorry.

ricfink

4:37 am on May 9, 2003 (gmt 0)

10+ Year Member



bad news: can't be done.

You can't read or write the value of a file input element.

this is a security issue.
(think about it, if you could write a value to it, the page author could automatically upload any file on the user's computer automatically.)

ShawnR

8:14 am on May 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Good news... ;)

You need to fix the following:

  • remove the square brackets from the names. i.e. use something like "Photofile_0" instead of "Photofile[0]". You could get "Photofile[0]" to work if you used something like: str = document.listor_form['Photofile[0]'].value; but I am not sure it is legal.
  • The picture is not a part of the form. Just use document.photo1.src
  • Use: document.listor_form.src = "file://localhost/" +str;

i.e. the following should work:


<html>
<head>
<title>hello world</title>

<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
function show_me(form_elem) {
document.pic1.src = "file://localhost/" + form_elem.value;
}
</script>
</head>
<body>
<form method="GET" name="upload_form" action="index.html" target="_top">
<input type="file" name="file_name" size="14" maxlength="256" value="">
<br>
<a class="button" href="javascript:show_me(document.upload_form.file_name);">Submit</a>
</form>
<img name="pic1" src="file://localhost/C:/Dove.gif"/>
</body>
</html>

No security issues I can see. All you are doing is reading the value of the file path which the user himself entered, not uploading anything.

Shawn

ShawnR

8:26 am on May 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PS Should work in IE or NN or whatever. No need to have different code for NN

jrfurman

12:31 pm on May 9, 2003 (gmt 0)

10+ Year Member



ricfink... I knew its a security issue to set the value, I'm just reading the value so that the user who is about to upload the file can preview image before commiting to the upload.

ShawnR,

Thanks for the code, didn't work in NN7.X I can get it to work if I iterate through the objects of the page and compare the name property to "Photofile[X]" and retrieve the value when I get a match. I works but really goes against my grain because it is so inefficent. I wouldn't mind changing the Photofile[X] to Photofile_X but I have a considerably amount of code already written in PHP that takes the array of images uploaded and plays around with them. And surely there is a way to crack the proper syntax.

Question about the localhost part of your code. Is localhost working own all PCs? Will someone who comes to my site with WIn 95 or Win 98 or an Apple be able to utilize localhost.? I thought it only worked if you had IIS or a webserver running on your machine.

Also it would be great if anyone could tell me how to consolidate those str.replace operation into one statement. I'd like to know just for my own sake of knowledge.

P.S. I had a very nice script written that works of MSIE, I'm just gettting a larger number of non-MSIE users to the site and I'm having to revamp my code to accommodate them.

Thanks again,

JR

jrfurman

1:29 pm on May 9, 2003 (gmt 0)

10+ Year Member



ShawnR,

str = document.listor_form['Photofile[0]'].value;
Seems to be proper, so the following works.

I'll play around with

for (k=0;k<2;k++) {
n = 'Photofile['+k+']';
str = document.listor_form[n].value;
str = str.replace(':','¦');
str = str.replace('\\','/');
str = str.replace(' ','%20');
eval ("document.listor_form.photo"+k+".src = \"file:///\" +str; ");
}

I'm going to play around with RegExp to replace the str.replace function....hopefully have time.

Thanks a bunch gang!

ShawnR

1:42 pm on May 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi JR

I tried it on a Win XP platform with NN4.7, NN7, Opera 6, Opera7, IE6. Seems to work in all except Opera 6.

The code I used is as previous post except:

<input type="file" name="file_name[1]" size="14" maxlength="256" value="">
<br>
<a class="button" href="javascript:show_me(document.upload_form['file_name[1]']);">Submit</a>

(for Opera 6, you need to strip out the quotes, else it tries to load up:

file://localhost/"C:/blah/blah.jpg"

instead of

file://localhost/C:/blah/blah.jpg
)

Not sure about Win 95, Win 98, apple mac, etc, but I don't have MS IIS installed, and I turned off the webserver on my local machine for the tests I did.

>>>"...And surely there is a way to crack the proper syntax..."
As I said, it works OK for me, but it is not proper syntax! The [nn] is used for indexing, so it is illegal to use it as part of the identifier. Have a look at the 'core attributes' section of the w3 html spec.

Sorry, I can't think of a way to substantially improve the str.replace. The escape() function will take care of the conversion of the space to %20. You could try have a play with using a 'transformation' type of regex for the other chars, and then pass the result to escape(), but I'm not sure if you will be able to substantially improve the elegance of it.

Shawn

[added: Oops: Seems I hadn't refreshed my screen, so I hadn't seen your previous post when I posted this one. Anyhow, they seem to be saying similar things...]

jrfurman

1:57 pm on May 9, 2003 (gmt 0)

10+ Year Member



Thanks Shawn,

I noticed you said N4.7, can I install it on my machine if I'm running NN7.X.

I have Win XP pro as well with Mozilla and IE installed.

Also do you have the different versions Opera installed on one machine?

I'm an old C guy who mainly coded for one platform, this multi platform and multiple plug-in world can be somewhat frustating enviroment to develop for.

Thank again,

JR

lowlander

6:35 pm on May 9, 2003 (gmt 0)

10+ Year Member



You can have all the browsers you want on your system, I have 9 at the moment, except for Internet Explorer of which there can be but one. You can have multiple versions of IE as long as they are on different partitions.
Get all the browsers you need from [browsers.evolt.org ]

ricfink

6:53 pm on May 9, 2003 (gmt 0)

10+ Year Member



Oops. Reading the value of a file input is OK. Writing to it is a no-no.
No point in assigning a value to the element in the HTML code, though. Won't do anything.

Sorry for missing the thrust of what you were trying to do.

Tested out ShawnR's variation and it worked for me. Worked in Mozilla (NN7) also.

DrDoc

12:49 am on May 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Otherwise, the easiest solution is to just give the input field an ID...

<script type="text/javascript">
changesrc() {
str = document.getElementById('photo_0').value;
str = str.replace(':','¦');
str = str.replace('\\','/');
str = str.replace(' ','%20');
document.photo1.src = "file:///" +str;
}
</script>
...
...
<form name="listor_form" method="post" ENCTYPE="multipart/form-data">
<img name="photo1" src="default.png"
<input id="photo_0" name="Photofile[0]" type="file">
<input id="photo_1" name="Photofile[1]" type="file">
<input name="preview" type="button" value="Netscape Users Click Here to Preview Images" onClick="changesrc();">
</form>