TheJediCowboy
TheJediCowboy

Reputation: 9222

What is Best way to obtain filename for HTML FILE Uploading on forms?

I need to obtain the String for the file that is being uploaded from forms to store in the database.

I am using the usual form input file element

 input type="file" name="some_name"

I found a couple JS scripts that allow me to do useless things like display the string in a dialog box, etc.

I need this as an element on the request object or as a hidden field on my page when the form is posted.

Upvotes: 1

Views: 101

Answers (2)

Pointy
Pointy

Reputation: 413702

You won't get a very useful value. Some browsers will only give you the final name part of the file path, while IE will give you a path with a bogus directory name.

I think that the "safe" fragment of the file name should already be passed in to you as part of the part header in the multipart post body.

Upvotes: 1

Daniel Vassallo
Daniel Vassallo

Reputation: 344281

You should be able to do something like this:

<form method="POST">
    <input type="file" name="some_name"
     onchange="document.getElementById('hidden_file').value = this.value;" />

    <input type="hidden" id="hidden_file" value="" />
    <input type="submit" />
</form>

I believe that will work in all browsers if you simply want to store the filename, and not the full path.

Upvotes: 2

Related Questions