简体   繁体   中英

Having Issue on Getting Uploaded File Name by jQuery

Can you please take a look at this demo and let me know why I am not able to get the name of the file(image)

 $('input:file').on('change', function(){ console.log($(this).prop("files")['name']); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="file" name="file" id="test" /> 

As you can see I am getting undefined in console.

$(this).prop("files") returns a FileList , not a single file.

You'd need to do:

$(this).prop("files")[0]['name']

See: https://developer.mozilla.org/en-US/docs/Web/API/FileList

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM