【发布时间】:2021-02-18 08:59:45
【问题描述】:
我已经完成了一个可以接受多个文件的上传文件,但我目前的问题是当用户拖放多个文件时,它接受除图像文件之外的其他文档类型文件。这是我的代码:
<div class="row">
<div class="col-lg-12">
<label class="fieldlabels">Photos:</label>
<!--input type="file" name="pic" accept="image/*"-->
<input type="file" class="form-control" id="file_name2" onchange="return fileValidation2()" name="file_name2" value="" title="Photos" accept="image/*" multiple="true"/>
<table class="table table-striped table-bordered" style="width:100%;" id="add_files2">
<thead>
<tr>
<th style="color:blue; text-align:center;">File Name</th>
<th style="color:blue; text-align:center;">Status</th>
<th style="color:blue; text-align:center;">File Size</th>
<th style="color:blue; text-align:center;">Type</th>
<th style="color:blue; text-align:center;">Action</th>
</tr>
</thead>
<tbody>
</tbody>
/table>
</div>
</div>
这是我迄今为止完成的脚本:
function fileValidation2(){
var fileInput = document.getElementById('file_name2');
var filePath = fileInput.value;
var allowedExtensions = /(\.jpg|\.jpeg|\.png|\.tiff|\.tif)$/i;
if(!allowedExtensions.exec(filePath)){
alert('Please upload file having extensions .jpeg/.jpg/.png/.tiff/.tif/.xlsx/.pdf/.xls/.docx/.doc/.bump only.');
fileInput.value = '';
return false;
}else{
//Image preview
if (fileInput.files && fileInput.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
document.getElementById('imagePreview').innerHTML = '<img src="'+e.target.result+'"/>';
};
reader.readAsDataURL(fileInput.files[0]);
}
}
}
当用户单击输入文件中的选择文件时,它工作得很好,但是当用户拖放多个包含除图像之外的文件时,它直到包含在内。有人知道如何解决这个问题吗?提前致谢。
https://i.stack.imgur.com/9iIbb.png
用户拖放多个文件时仍会包含 zip 文件和其他文件。
【问题讨论】:
-
你至少可以做“便宜的版本”,然后在你的循环中再做一次,你以前用过的
if(!allowedExtensions.exec(fileInput.files[0].name)){ return; } -
你能告诉我它是如何工作的吗?谢谢
标签: javascript html