Wai Yan
Wai Yan

Reputation: 427

How to get file name auto when I browse a file to upload?

I want to create a upload form. When I browse a file.Example. C:\mypicture\winter.jpg I want to get file name and fill in the text box 'winter.jpg' Automatically.

enter image description here Thank .

Upvotes: 0

Views: 1034

Answers (2)

PeeHaa
PeeHaa

Reputation: 72729

See the demo in pure JS: http://jsfiddle.net/VaWDP/2/

<input type="file" id="file">
<input type="text" id="name">​

var input = document.getElementById('file');
var name = document.getElementById('name');

if (input.addEventListener) {
    input.addEventListener('change', add_filename);
} else if (input.attachEvent) {
    input.attachEvent('onchange', add_filename);
}

function add_filename()
{
    name.value = input.value.split(/\\|\//).pop();
}

​See the demo in jQuery: http://jsfiddle.net/SsW6t/

(function() {
    $('#file').change(function() {
        $('#name').val($(this).val().split(/\\|\//).pop());
    });
})(jQuery);​

Upvotes: 2

Jovan Perovic
Jovan Perovic

Reputation: 20201

It's not possible. Websites should not be able to predetermine location of a data on your local disk.

If such thing would be possible some questions arise like:

  • what if file does not exists
  • what if file is security sensitive (e.g. encryption keys)

@PeeHaa commented that it is and therefore if such example exists it's clearly a security issue. This JSFiddle example does not work for me...

Upvotes: 0

Related Questions