JQuery: 获取从 < input type = “ file”/> 中选择的文件名

这段代码应该可以工作在 在 IE 中(甚至不用 Firefox 测试) ,但是它不能。 我想要的是显示附加文件的名称。有什么帮助吗?

<html>
<head>
<title>example</title>
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript">
$(document).ready( function(){
$("#attach").after("<input id='fakeAttach' type='button' value='attach a file' />");
$("#fakeAttach").click(function() {
$("#attach").click();
$("#maxSize").after("<div id='temporary'><span id='attachedFile'></span><input id='remove' type='button' value='remove' /></div>");
$('#attach').change(function(){
$("#fakeAttach").attr("disabled","disabled");
$("#attachedFile").html($(this).val());
});
$("#remove").click(function(e){
e.preventDefault();
$("#attach").replaceWith($("#attach").clone());
$("#fakeAttach").attr("disabled","");
$("#temporary").remove();
});
})
});
</script>
</head>
<body>
<input id="attach" type="file" /><span id="maxSize">(less than 1MB)</span>
</body>
</html>
204463 次浏览

It is just such simple as writing:

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

Anyway, I suggest using name or ID attribute to select your input. And with event, it should look like this:

$('input[type=file]').change(function(e){
$in=$(this);
$in.next().html($in.val());
});
//get file input
var $el = $('input[type=file]');
//set the next siblings (the span) text to the input value
$el.next().text( $el.val() );

I had used following which worked correctly.

$('#fileAttached').attr('value', $('#attachment').val())

Add a hidden reset button :

<input id="Reset1" type="reset" value="reset" class="hidden" />

Click the reset button to clear the input.

$("#Reset1").click();
$('input[type=file]').change(function(e){
$(this).parents('.parent-selector').find('.element-to-paste-filename').text(e.target.files[0].name);
});

This code will not show C:\fakepath\ before file name in Google Chrome in case of using .val().

<input onchange="readURL(this);"  type="file" name="userfile" />
<img src="" id="blah"/>
<script>
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();


reader.onload = function (e) {
$('#blah')
.attr('src', e.target.result)
.width(150).height(200);
};


reader.readAsDataURL(input.files[0]);
//console.log(reader);
//alert(reader.readAsDataURL(input.files[0]));
}
}
</script>

The simplest way is to simply use the following line of jquery, using this you don't get the /fakepath nonsense, you straight up get the file that was uploaded:

$('input[type=file]')[0].files[0]; // This gets the file
$('#idOfFileUpload')[0].files[0]; // This gets the file with the specified id

Some other useful commands are:

To get the name of the file:

$('input[type=file]')[0].files[0].name; // This gets the file name

To get the type of the file:

If I were to upload a PNG, it would return image/png

$("#imgUpload")[0].files[0].type

To get the size (in bytes) of the file:

$("#imgUpload")[0].files[0].size

Also you don't have to use these commands on('change', you can get the values at any time, for instance you may have a file upload and when the user clicks upload, you simply use the commands I listed.

<input onchange="readURL(this);"  type="file" name="userfile" />
<img src="" id="viewImage"/>
<script>
function readURL(fileName) {
if (fileName.files && fileName.files[0]) {
var reader = new FileReader();


reader.onload = function (e) {
$('#viewImage')
.attr('src', e.target.result)
.width(150).height(200);
};


reader.readAsDataURL(fileName.files[0]);
}
}
</script>