Can't get value of input type="file"?

JavascriptJqueryFileInput

Javascript Problem Overview


I have a <input type="file" id="uploadPicture" value="123">

When I'm using: alert($("#uploadPicture").val());

It alerts an empty dialog.

Javascript Solutions


Solution 1 - Javascript

@BozidarS: FileAPI is supported quite well nowadays and provides a number of useful options.

var file = document.forms['formName']['inputName'].files[0];
//file.name == "photo.png"
//file.type == "image/png"
//file.size == 300821

Solution 2 - Javascript

You can read it, but you can't set it. value="123" will be ignored, so it won't have a value until you click on it and pick a file.

Even then, the value will likely be mangled with something like c:\fakepath\ to keep the details of the user's filesystem private.

Solution 3 - Javascript

You can get it by using document.getElementById();

var fileVal=document.getElementById("some Id");
alert(fileVal.value);

will give the value of file,but it gives with fakepath as follows

c:\fakepath\filename

Solution 4 - Javascript

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

That'll get you the file selected.

However, you can't set the value yourself.

Solution 5 - Javascript

You can't set the value of a file input in the markup, like you did with value="123".

This example shows that it really works: http://jsfiddle.net/marcosfromero/7bUba/

Solution 6 - Javascript

It's old question but just in case someone bump on this tread...

var input = document.getElementById("your_input");
var file = input.value.split("\\");
var fileName = file[file.length-1];

No need for regex, jQuery....

Solution 7 - Javascript

array of files

let files = document.getElementById('your_input_id').files

first file

let file = document.getElementById('your_input_id').files[0]

show image onload

const reader = new FileReader()

let files = document.getElementById('your_input_id').files
reader.onload = async (event) => {
    document.getElementById('preview').setAttribute('src', event.target.result)
}
reader.readAsDataURL(files[0])

Solution 8 - Javascript

don't give this in file input value="123".

  $(document).ready(function(){  

      var img = $('#uploadPicture').val();

  });

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDanpeView Question on Stackoverflow
Solution 1 - JavascriptDmytro VyprichenkoView Answer on Stackoverflow
Solution 2 - JavascriptQuentinView Answer on Stackoverflow
Solution 3 - JavascriptNaresh.PView Answer on Stackoverflow
Solution 4 - JavascriptJack MarchettiView Answer on Stackoverflow
Solution 5 - JavascriptmarcosfromeroView Answer on Stackoverflow
Solution 6 - JavascriptBozidar SikanjicView Answer on Stackoverflow
Solution 7 - JavascriptSamculoView Answer on Stackoverflow
Solution 8 - JavascriptMd ShahriarView Answer on Stackoverflow