我开始编写脚本,允许用户点击图标并选择要上传的图像。 我想允许用户上传多个文件。我该怎么做(使用图标按钮打开浏览器对话框)
这就是我所做的:
jsfiddle.net/wsurube2 /
答案 0 :(得分:0)
带有type =" file"的HTMLInputElement;有一个.multiple属性,指示输入是否可以有多个值。然后,您可以使用element.files集合按索引访问文件。
input.files [指数]
https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/multiple
HTML:添加多个属性。
<input type='file' name='file[]' class=' form-control file-field hide' max_files_upload='2' accept="image/jpg,image/png,image/jpeg,image/gif" style="display: none" multiple/>
http://jsfiddle.net/SeanWessell/wsurube2/1/
// validation for images
$(function() {
$('input.file-field').on('change', function(e) {
alert(this.files.length);
var filename = $(this).val();
if (!(/\.(gif|jpg|jpeg|tiff|png)$/i).test(filename)) {
this.value = '';
$(this).closest('form').find('.fileNameBox').html("<span class='valid_msg'>Images only!</span>");
}
else
$(this).closest('form').find('.fileNameBox').html("IMAGE: " + filename + " <button class='clear_file btn btn-default btn-xs'>REMOVE</button>");
});
});