3

What am I trying to do?

I am trying to validate a file after it has been selected using the input type="file" tag. One of the tests is to make sure that the filename is less than 200 characters long.

What have I done so far?

http://jsfiddle.net/joanferns00/ajeec780/4/ When I try to upload the following sample file, (file of filename length 230) 234_aaaaa6JHmFop6Va6JHmFRsdK7fxn1HtVkpl5UREy7cn4yC4hlHuW87qDp2fEg3YQlZCETrkBbLqIgtAqlklyahRIH0hCzSUO234_op6Va6JHmFRsdK7fxn1HtVkpl5UREy7cn4yC4hlHuW87qDp2fEg3YQlZCETrkBbLqIgtAqlklyahRIH0hCzSUOI3YQlZCETrkBbLqIgtAqlklkBbLqIgtAqlkl.txt

function getLength() {
     //alert(.length);
     var fullPath = document.getElementById("myfile").value;
     if (fullPath) {
         var startIndex = (fullPath.indexOf('\\') >= 0 ? fullPath.lastIndexOf('\\') : fullPath.lastIndexOf('/'));
         var filename = fullPath.substring(startIndex);
         if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) {
             filename = filename.substring(1);
         }
         alert(filename.length);
     }
}

What do I expect?

I expect IE9 to alert the length of the text like it does in Chrome and FireFox

What is happening instead?

Firefox and chrome gives me a value of 230 however IE9 doesnt even allow me to upload the file.

Is there a way for IE9 to recognize this file and alert the filename length like Chrome and Firefox?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
c f
  • 196
  • 2
  • 14
  • http://stackoverflow.com/questions/14898799/internet-explorer-fileupload-control-doesnt-recognize-files-with-long-file-nam – epascarello Jan 10 '15 at 04:29

1 Answers1

4

Windows limits the length of a filepath to 255 characters (filepath+name, including extension, and the .), + 3 extra characters for C:\

Source: http://vlaurie.com/computers2/Articles/filenames.htm

I encounter it all the time when trying to unzip big nested zip files.

IE9 makes it worse because it includes the full path for the filename for file input elements, including the opening "C:\".

Source: http://support.softartisans.com/kbview_892.aspx

So on windows, the theoretical limit on a filename is 258 chars, including the C:\. But when IE9 grabs that file, it shows the complete path, including the C:\, so the largest filename you can upload to IE9 is 255 chars, including the C:\

So for example, this will NOT upload

C:\1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890axxx.txt

but this will

C:\1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890a.txt

and windows would not even let you name a file this:

C:\1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890axxx00000.txt

because its just too long.

Chances are, your file was somewhere in that "magical range" of being greater than 255, so not allowed by IE, but smaller than 259, so still allowed by Windows. Firefox and Chrome don't care, nor do newer versions of IE. There isn't anything you can do for IE9.

chiliNUT
  • 18,989
  • 14
  • 66
  • 106
  • Thanks. Can javascript code to catch this case in IE9? in other words, I would like the user to know if their filename/filepath is too long instead of a blank input field showing up when such a file is selected. – c f Jan 12 '15 at 18:26
  • IE9 just silently fails, so generally, no. Something to investigate would be to try and capture the event of the user selecting a file, and then seeing if it changed or not, but this is tricky, because I think you can only capture the click even, not the actual file selection event – chiliNUT Jan 12 '15 at 19:07