Richard Castera

Application Developer/Designer
  • Home
  • About
  • Projects

Get the filename from upload form using Javascript

I needed a way to access the filename of a file being uploaded or attached using the input file from a form. So, I created a nice little function to achieve this. Hopefully it comes in handy for someone.

<script type="text/javascript">
function getNameFromPath(strFilepath) {
    var objRE = new RegExp(/([^\/\\]+)$/);
    var strName = objRE.exec(strFilepath);
    if (strName == null) {
        return null;
    }
    else {
        return strName[0];
    }
}
</script>
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Don't forget to Subscribe and Follow!

Be sure to subscribe to the feed and follow me on Twitter for more insights and resources!

5 Responses to “Get the filename from upload form using Javascript”

  1. Comment #1 - Permalink
    Rod Smith
    02/09/2009

    Hi Richard
    This looks like the function I require but maybe you can confirm…
    I’ve produced a website for our local camera club and a few of us are uploading our pictures but the ftp process is a bit tricky for some less computerate users.
    So I thought I’d use the html file upload on a basic form for them.
    Unfortunately when they select a file it only shows the first part of the path and they’re not sure it’s the correct picture. I’d like to show just the filename, (and extension), below the select field so they can be sure about what they are uploading.
    I know I can’t check filesizes for them with javascript but could I check that the name fits our layout? Basically I’m telling everyone to call their pictures p1.jpg, p2.jpg etc so they drop into the gallery in the right place.
    Many Thanks
    ROD

  2. Comment #2 - Permalink
    Richard
    05/03/2009

    Hey Rod,

    Sorry it took so long to reply. Yes this is what you are looking for. It will display the filename of the file they want to upload.

  3. Comment #3 - Permalink
    Daniel Messano
    06/25/2009

    Thank you. Given enough time I could have come up with something like this, but i don’t need to because you did :)

  4. Comment #4 - Permalink
    Naveen
    05/21/2010

    Thank you Richard.
    This came in handy for me!

  5. Comment #5 - Permalink
    Richard
    05/31/2010

    @Naveen, Thanks!

It's Your turn

Click here to cancel reply.
You

CAPTCHA Image
CAPTCHA Audio
Refresh Image



About The Author

Richard is a Web Developer currently working for SankyNet. He is available for freelance work. Visit Shifting Ideas for inquiries. Thanks!
  • Premium Email Templates

    Social Profiles

  • Twitter
  • Facebook
  • LinkedIn
  • Digg
  • Google
  • GitHub

    Last Tweet

  • Beware the heat-seeking Nerf machine-gun coming to a cubicle near you http://om.ly/svHF #fb
  • Follow me
  • Archives

    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • September 2008
    • August 2008
    • July 2008
    • June 2008
  • Categories

    • AJAX
    • Apache
    • Books
    • Drupal
    • Ecommerce
    • Flash
    • Google
    • Javascript
    • jQuery
    • Magento
    • Marketing
    • Mootools
    • My Thoughts
    • MySQL
    • News
    • Photoshop
    • PHP
    • Prototype
    • SEO
    • Web Resources
    • Wordpress
  • My Favorites

    • 37 Signals
    • Andrew Warner
    • Chris Coyier
    • Chris Shiflett
    • Collis Ta'eed
    • Development Seed
    • Drupal
    • HubSpot
    • James Padolsey
    • Joel On Software
    • John Resig
    • jQuery
    • Kevin Rose
    • Life Hacker
    • Magento
    • Mashable
    • Matt Cutts
    • Matt Ryan
    • MySQL
    • NetTuts
    • Noupe
    • Photoshop Tutorials
    • PHP
    • Smashing Magazine
    • Tech Crunch
    • WoorkUp
    • Wordpress

2010 © Copyright. Richard Castera - All Rights Reserved.

Top