0

you cant change the value of the input type = 'file' in a form because of security reasons. But is it possible to read the value at all with javascript to then check extensions and validate the form? or will that also be a security breach?

Some examples would ease my pain...

thanks

  • 1
    Yes, you can read the value to validate it: http://stackoverflow.com/questions/71944/how-do-i-validate-the-file-type-of-a-file-upload – Crescent Fresh Nov 10 '09 at 17:59
  • 1
    Once you've read/validated the filename, does that help much? A user can name a file anything. – Ben Dunlap Nov 10 '09 at 18:01
  • Consider using http://swfupload.org ? which is Flash file uploader - it provides validation of Type, Max size, etc. plus progress bars and CANCEL button – Kristen Nov 10 '09 at 18:04

3 Answers3

2

You can only read the name of the file and it's extension, so eg: 'file.zip'. It won't tell you the path, unless you are using IE.

Here's a simple example:

<input type="file" onblur="alert(this.value)" />

This will give you the filename + extension..

Waleed Amjad
  • 6,716
  • 4
  • 35
  • 35
  • ok, so how would i do that, that would be enough for me... some javascript function, or just file.value work? –  Nov 10 '09 at 17:58
1

Sure you can read the value. Just read and validate it as you do for every other form element. Have you tried it yourself anyway? This particular question doesn't make me think so. A bit more programming effort from your side is highly appreciated.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

You can read the extension of the filename from input.value, sure. But it won't do you any good. You don't know what file extensions are mapped to the various filetypes under the user's operating system, and you don't even know the user's OS uses filename extensions for filetyping. OS X and Linux users are quite likely to submit files with no file extension at all.

There's nothing worse than an idiotic file upload that won't accept your JPEG because it thinks JPEGs have to end in ‘.jpg’.

bobince
  • 528,062
  • 107
  • 651
  • 834