31

From what I've ready you should be able to use the 'value' property of a file input field to get the currently set path of that field. But when I do this:

 $('#fileinput').value()

I get 'undefined'. The ID of the field is set to "fileinput" I'm sure. Does anyone have any thoughts on why this might not be working for me?

And by the way, this works:

var d = document.getElementById('AttachmentFile');
alert(d.value);

So I guess this has something to do with the way jQuery works that I don't fully understand.

Thanks!

0

8 Answers 8

63

You need to use val rather than value.

$("#fileinput").val();
Sign up to request clarification or add additional context in comments.

1 Comment

Works for me in Chrome 10 and 11.
27

In Chrome 8 the path is always 'C:\fakepath\' with the correct file name.

Comments

6

its not .val() if you want to get file /home/user/default.png it will get with .val() just default.png

Comments

3

I don't think there is any real legitimate way to access this via the DOM. It would be a security risk that browsers have of late locked down on to prevent drive-by uploads.

3 Comments

I think the security risk would be setting this value, not reading it.
It would all depend on the browser.
Having just tested it you get different results depending on where you're running from. Running the file locally you get a full path, running from a webserver you just get the name of the selected file, which makes sense from a security point of view.
2

I think it should be

 $('#fileinput').val();

Comments

1

Jquery works differently in IE and other browsers. You can access the last file name by using

alert($('input').attr('value'));

In IE the above alert will give the complete path but in other browsers it will give only the file name.

1 Comment

It's not jQuery that is behaving differently but the underlying DOM api.
0

Could you also do

$(input[type=file]).val()

Comments

0

I've tried this and it works:

 yourelement.next().val();

yourelement could be:

$('#elementIdName').next().val();

good luck!

1 Comment

To improve the quality of your post, please include how/why this code will solve the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.