2

I have a form upload

I need a jQuery function that will write in a hidden text input what is written in the input file.

So I have :

<input class="addFile" id="field2" name="fileLyric" type="file" />

Whatever will be written there by the uploading (obviously the file path) should be then automatically written in a hidden text input

<input id="field2hidden" name="fileLyrichidden" type="text" style="display:none"/>

Is there a way to achieve this?

Thank you!

1
  • 1
    you can't get the entire file path due to security reasons but you can get the filename and its extension in the hidden field. Commented Jun 17, 2011 at 7:57

4 Answers 4

9

This will do it, but as the others already said, this will not be the full path for security reasons.

Add a change listener, so the value will be updated if the user has selected a file.

$('#field2').change( function() {
    $('#field2hidden').val($(this).val());
});

Here is a jsFiddle for demonstration. Try it in different browsers and you'll see what you get as path.

Sign up to request clarification or add additional context in comments.

Comments

2

You can't take the actual path of the file from the input box. Most browsers will put a fake path so that you can't use it. This is a security feature of the browser.

Comments

2
$('#field2hidden').val ($('#field2').val ())

Wouldn't that do it?

EDIT: It would get the filename but the path section is deliberately obscured. You can't (or at least shouldn't) be able to get the full path in javascript as it could expose information about the user's filesystem.

Comments

1

You can not reliable get the path used to upload a file (Chrome will say the path is C:\fakepath\.... Best you can do is get the filename.

Comments

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.