JavaScript code to get FileName from File Inputbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bsprogs
    New Member
    • Jan 2007
    • 6

    #1

    JavaScript code to get FileName from File Inputbox

    Alright, I'm very new to JavaScript but I'm fair with PHP.
    I'm working on an uploader script and what I'm trying to do is take the file name from the file input box.

    I looked at string manipulation in terms of using indexOf, substr, substring, etc... and I can't figure it out for the life of me!

    Here is what I'm trying to do:

    Example:
    I select the file:
    C:\Documents and Settings\BSPROG S\Desktop\DeVry \Previous work\BIS 440\ESSS_Case_S tudy.zip

    I want to use the document.write command to write:
    ESSS_Case_Study .zip

    I've looked through my "JavaScript : The Definitive Guide" book from O'Reilly but luck.

    Thanks for your help.

    Steven
  • vishnuvarthan
    New Member
    • Jan 2007
    • 5

    #2
    First, you split the string into string array using '\' and get its array length. Then get the penultimate string value.... Hope it works....Find below the example code..

    var strPath = C:\Documents and Settings\BSPROG S\Desktop\DeVry \Previous work\BIS 440\ESSS_Case_S tudy.zip
    var strPathArray = strPath.split(' \');
    var strFileName = strPathArray[strPathArray.le ngth-1];

    You can get the name in strFileName variable....

    Comment

    • bsprogs
      New Member
      • Jan 2007
      • 6

      #3
      Originally posted by vishnuvarthan
      First, you split the string into string array using '\' and get its array length. Then get the penultimate string value.... Hope it works....Find below the example code..

      var strPath = C:\Documents and Settings\BSPROG S\Desktop\DeVry \Previous work\BIS 440\ESSS_Case_S tudy.zip
      var strPathArray = strPath.split(' \');
      var strFileName = strPathArray[strPathArray.le ngth-1];

      You can get the name in strFileName variable....
      That worked perfectly! Thank you. :)
      As I learn more of how the JavaScript functions work, I'll get the hang of it, haha.

      Comment

      Working...