Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

HTML <input type=file/> Validation

Options
  • 01-09-2006 3:23pm
    #1
    Registered Users Posts: 2,492 ✭✭✭


    Hi,

    Im using this tag so that a user can use the Browse button and select a file.

    However, the user can also type the name of the file into the field.

    Its a web app and I need to put validation into my servlet to check if the file exists. The file paths will be in the form of \\machine123\dir1\file1.txt for example.

    Validation needs to be done on the server, any idea how to go about this?

    Thanks for you help guys....


Comments

  • Registered Users Posts: 4,074 ✭✭✭muckwarrior


    I'm a bit confused. Is the user suppose to select a file on their local machine or on the server? A bit more info on how it works would be useful.


  • Registered Users Posts: 6,414 ✭✭✭kdouglas


    well if the user types in a non-existent file, then surely the browser will pick up on this and throw an error, or when submitted to the server there just wont be any data available?


  • Registered Users Posts: 568 ✭✭✭phil


    You sound like you're confused.

    Servlets run on the remote web server away from the client. If a client selects a file to upload, the servlet cannot check if the file exists or not. It can only work with the data presented to it by the browser.

    You need to explain a little better what it is you're doing / trying to do.


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    phil wrote:
    If a client selects a file to upload, the servlet cannot check if the file exists or not.
    Of course server can check if the file exists or not, just as it can validate any other variable after the form has been submitted.
    Validation needs to be done on the server, any idea how to go about this?
    If no valid file has been submitted then the upload (separate to the pathname - which is also sent) will essentially be null, if it is simply not a valid file then you’ll have to parse through the file to identify and thus validate it.


  • Registered Users Posts: 5,618 ✭✭✭Civilian_Target


    Hi,

    Im using this tag so that a user can use the Browse button and select a file.

    However, the user can also type the name of the file into the field.

    Its a web app and I need to put validation into my servlet to check if the file exists. The file paths will be in the form of \\machine123\dir1\file1.txt for example.

    You can't do that. You can't do any validation until you receive the POST array from your client. There's no way you can actually find out what the path of the file on the client machine is from that data, only the file name
    Validation needs to be done on the server, any idea how to go about this?

    Thanks for you help guys....

    Sounds like you're doing this in Enterprise Java in which case your best bet is to use the Commons File Upload library ( http://jakarta.apache.org/commons/fileupload ) to deal with all your file handling. The isMultipartContent(req) method will check that you're receiving a valid POST array from the client for you and the isFormField() method will return true for text content (such as mime headers and client info) and false for binary data (ie. the file you want) and you can use that to extract and deal with the file as you see fit. I strongly recommend you check it out.


  • Advertisement
Advertisement