5 Replies Latest reply: Feb 16, 2010 10:27 PM by 667844 RSS

    Restricting length of file name of the file to be uploaded in file browse.

    667844
      Hi,

      I have got a file browse item. When the file name is more than 74 characters, it goes to an error page. Can this be avoided?
      To prevent this, I tried using a javascript
      function doSubmitWithProgress()
      {
      if (document.getElementById('P202_FILE_INPUT').value.length > 70){
      alert('File name should be less than 70 characters in length.');
      object.focus();
      }
      else
      {$x_Hide('wwvFlowForm');
      $x_Show('InProgress');
      doSubmit('UPLOAD_FILE');
      }
      }
      But this is considering the path of the file as well and hence causing an issue.
      So could you please help me out with this issue.
      If there is a way to avoid going to error page if file name is big that would be great. Otherwise, if there is a way to check just the length of the file name and trigger an alert if it is more than 70 characters, that would help me too.

      Thanks,
      Vignesh
        • 1. Re: Restricting length of file name of the file to be uploaded in file browse.
          728727
          Vignesh,

          You can use the javascript str.split()* method to break the full path down into an array of it's individual pieces and then check the length of the last piece (which ends up to be the file name).

          For instance.
          function doSubmitWithProgress()
          {
            // Create a variable to hold the full file name with path
            var fullFileName = document.getElementById('P202_FILE_INPUT').value;
          
            // Now create an array that contains the individual pieces of
            // the file name split on the "/" boundaries
            var myArray  = fileName.split('/');
          
            // The last variable contains the length of the last item in the array
            // which ends up to be our actual file name.
            //
            // NOTE: We have to use myArray.length-1 because JavaScript
            // Arrays start at 0
            var fileNameLength = myArray[myArray.length-1].length;
          
            if (fileNameLength > 70)
            {
              alert('File name should be less than 70 characters in length.');
              object.focus();
            }
          else
            {
              $x_Hide('wwvFlowForm');
              $x_Show('InProgress');
              doSubmit('UPLOAD_FILE');
            }
          }
          Hope this helps

          Doug Gault
          [SUMNEVA.COM|http://www.sumneva.com/]
          • 2. Re: Restricting length of file name of the file to be uploaded in file browse.
            667844
            Thanks a lot Doug Gault.
            That solved my problem. :)
            • 3. Re: Restricting length of file name of the file to be uploaded in file browse.
              667844
              Hi Doug Gault,

              The snippet you provide works fine in Firefix but is not working in IE. When I load this page in IE, even after going through the snippet, the entire file path is present in the variable and hence an alert appears that the file name is more than 70 characters. Any suggestion as to why this is happening?

              Thank,
              Vignesh
              • 4. Re: Restricting length of file name of the file to be uploaded in file brow
                fac586
                The issue is that this code
                  // Now create an array that contains the individual pieces of
                  // the file name split on the "/" boundaries
                  var myArray  = fileName.split('/');
                has the wrong path separator for Windows. Use backslash instead:
                  // Now create an array that contains the individual pieces of
                  // the file name split on the "\" boundaries
                  var myArray  = fileName.split('\\');
                double backslash is necessary because '\' is the JavaScript escape character, so it needs to be doubled to indicate that that is the character we want.

                However, also be aware that for security/privacy reasons recent versions of browsers do not expose the file path via the .value method. Firefox, Safari and Chrome only provide the filename. IE6 & IE7 yield the path in Windows format. IE8 and Opera, in an effort not to "break the web" (i.e. to bend over backwards to keep poorly coded and non-interoperable Windows sites working) have adopted an irritating approach of replacing the path with a wholly imaginary "C:\fakepath\".

                So your validation may have to cope with up to 3 scenarios, depending on the browsers that need to be supported: only get the filename (FF/Safari/Chrome); get the full path (IE6/7/8 in some security configurations/older versions of all browsers); get a fake path (Opera/IE8 in other security configurations).

                For more information see:

                http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2009-March/018980.html

                http://blogs.msdn.com/ie/archive/2009/03/20/rtm-platform-changes.aspx

                (In 2009 MS are still concerned that pathname segments are limited to 8 characters?)
                • 5. Re: Restricting length of file name of the file to be uploaded in file brow
                  667844
                  Thanks a lot for that piece of information. Now things work fine in IE6, IE7 and Firefox. And as a matter of fact, I need just the file name and not the path. So guess, this is the exact thing I wanted.

                  Thanks,
                  Vignesh