Ed Jay wrote:
> Captain Paralytic wrote:
[...]
>>> I'm hopeful, but nothing I've tried has worked. I've tried
>>> document.forms["form1"].elements["myFile0"].value but it always
>>> comes up empty.
>>
>> Your form is called "form2" not "form1"
>
> Thanks. My typo error.
First of all, apologies for commenting on this so late. When the thread was
fresh, I was actually having a field day in Peak District.
The code, with the correction, indeed works, for some values of "works".
It's a bit outdated coding style, since these days we normally use a more
direct approach:
document.getElementById("myFile0").value
However, what you get depends on the browser and its settings. This is one
of the areas where security settings may have great impact, and they might
prevent getting the file path, as it can be considered to be in the area of
privacy of the computer or her user. For example, on IE 8 you would get
something like
C:\fakepath\foobar.zap
where "fakepath" appears literally, i.e. the actual path has been masked
out. On Firefox, you would get just
foobar.zap
i.e. the relative filename without hinting what it is relative to.
Moreover, although file input was originally defined in HTML drafts and
specifications as allowing multiple files per one <input type="file">,
browsers generally ignored that and supported only one file per element.
Typically, authors have solved this problem by providing multiple <input
type="file"> elements, possibly generating them on the fly with client-side
JavaScript (so that whenever a new file has been selected, a new file input
dialog appears).
The fancy HTML 5 stuff is in this matter, as usual, rather useless for the
time being. Firefox supports multiple file input when the multiple attribute
is used, as per the HTML 5 draft (so it graciously acts by published
specifications if you throw in some attributes that are invalid according to
published specifications), but IE doesn't. So you might get lured into
thinking you have created multiple file input control when you actually
provide just single file input to most users. Moreover, Firefox only returns
the name of the first file when there are multiple files.
Looking at the current HTML 5 draft, I notice the files attribute which is
supposed to return a FileList object of selected files. So to get the names
of the selected files you would use
document.getElementById("myFile0").files[i].name)
where i is an index to the list of files, starting from 0. If you only wish
to get the number of files, you can use
document.getElementById("myFile0").files.length
Of course such an expression is unsafe, as the browser might not have
implemented this part of the HTML 5 draft. You can use e.g.
if(document.getElementById("myFile0").files) { ... } else { ... }
so that the then part uses the information as above whereas the else part
assumes that the number of files cannot be known (and is most probably
either 1 or 0, though you can probably distinguish between 1 and 0 by
checking whether document.getElementById("myFile0").value is defined or not.
--
Yucca,
http://www.cs.tut.fi/~jkorpela/