SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get file path and file name of input type="file"

    alright suppose i have the following.

    Code HTML4Strict:
    <input type="file" name="upload"  />
    <input type="text" name="discription"  />
    <input type="image" onclick="addFile();" src="images/addfile.jpg" name="add" alt="Add file" />

    Here when user clicks add button, it gets added dynamically in to a div. Which all good and nice, i have very well done so.

    But here is the problem, on that parent div there are children, children divs contains the input which displays it's file name and hidden div containing file path, and another hidden input containing the description. As user adds files, they keep getting added in a list fashion.

    I plan to get process the values through array on the form submission. By the way there is only form on the parent div containing the child divs. not where the file input is.

    What i want is that, i want to get the file path in javascript string as well as file name in another string. How can i go on about doing this.

    Hope i was clear in explaining it.

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't count on having access to the file's path from Javascript due to security considerations. Some browsers currently return the file name AND path from the .value property of the input type=file object, but most modern browsers only return the filename.

  3. #3
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found out how to do it. I took the file path and from it i extracted the the file name using lastIndexOf("\\") and substring combination. Thanks for all your help.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    That will only work in the browsers with lower levels of security that actually provide that info. Most browsers don't give you the ability to be able to extract it because they only give you the file name s it can compromise the security of the owner's computer if you know what operating system and directory structure they are using.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well it works perfectly fine on Internet Explorer 7, Firefox, opera and Safari. All latest versions. could you name the browsers that you think it wont work with

  6. #6
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cssExp View Post
    I found out how to do it. I took the file path and from it i extracted the the file name using lastIndexOf("\\") and substring combination. Thanks for all your help.
    That will work on Windows systems, but not on a Mac, a Linux system, an OpenVMS system, etc.
    Birnam wood is come to Dunsinane

  7. #7
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I haven't taken those into consideration, that could be a problem as i wasn't it to be compatible mainly to windows but also with others, any idea what kind of errors that might occur in systems other than windows?

  8. #8
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem would be that they use other path separators than the backslash character for which you are looking.

    Linux, Mac OS X and other Unix-style operating systems use a forward slash as the path separator. Other operating systems may have other separators. (OpenVMS uses disk:[dir.subdir]file.ext, if I remember correctly. Not that it's all that likely that you'll have visitors running OpenVMS. )
    Birnam wood is come to Dunsinane


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •