如何为输入类型文件提供验证以接受多个文件的jpg和pdf文件 [英] how to give validations for input type file to accept jpg and pdf files for multiple files
问题描述
我在文件浏览按钮中选择第二个文件时无法给出验证,对于第一个文件,我可以应用验证,这是我的代码在mvc
I am not able to give validations while selecting second file in file browse button, for first file i am able to apply validations, here is my code in mvc
<div id="fileUploadContainer">
<div id="uploadContainer" style="height:30px" >
<input type="file" id="file1" name="file1" onchange="ValidateSingleInput(this);" />
</div> </div>
<a id="btnAdd" href="#">Add Another</a>
点击超链接(Add Another)我正在调用Jquery函数添加另一个文件上传按钮,jquery函数是
By clicking on hyperlink(Add Another) I am Calling Jquery function to add another fileupload button,The jquery function is
$('#btnAdd').click(function (e) {
e.preventDefault();
var htmlFormatDiv = $("<div id='uploadContainer' style='height:30px'></div>");
var htmlFormatFile = $("<input type='file' />");
var totalFileCount = $("#fileUploadContainer").children("div").length;
var fuData = document.getElementById('<%= file1.ClientID %>');
htmlFormatFile.attr("id", "file1");
htmlFormatFile.attr("name", "file1");
htmlFormatDiv.attr("id", "uploadContainer");
htmlFormatDiv.append(htmlFormatFile);
$("#fileUploadContainer").append(htmlFormatDiv);
});
});
像这样我要添加多个文件,现在我想给出验证文件只接受pdf和jpg,它不应超过3 mb,我添加了一个java脚本函数来执行此操作,但我只能对第一个文件进行验证,它不适用于添加了超链接的剩余文件(添加按钮),我使用的java脚本函数是:
Like this I am adding multiple files, now i want to give validations for the files to accept only pdf and jpg and it should not exceed 3 mb, I added one java script function to do this, but I am able to give validations to only the first file, It's not working for remaining files which are added with hyperlink(Add Button),The java script function which i used is:
<script type="text/javascript" language="javascript">
var _validFileExtensions = [".jpg", ".jpeg", ".pdf"];
function ValidateSingleInput(oInput) {
if (oInput.type == "file") {
var sFileName = oInput.value;
if (sFileName.length > 0) {
var fsize = sFileName.size;
if (fsize > 1048576) //do something if file size more than 1 mb (1048576)
{
alert(fsize + " bites\nToo big!");
}
var blnValid = false;
for (var j = 0; j < _validFileExtensions.length; j++) {
var sCurExtension = _validFileExtensions[j];
if (sFileName.substr(sFileName.length - sCurExtension.length, sCurExtension.length).toLowerCase() == sCurExtension.toLowerCase()) {
blnValid = true;
break;
}
}
if (!blnValid) {
alert("Sorry, " + sFileName + " is invalid, allowed extensions are: " + _validFileExtensions.join(", "));
oInput.value = "";
return false;
}
}
}
return true;
}
</script>
如果有人遇到此问题请帮助,(如何对添加了超链接的多个文件进行验证)
If any one faced this problem please help,(How to do validations for multiple files which are added with hyperlink)
推荐答案
('#btnAdd')。click(function(e){
e.preventDefault();
var htmlFormatDiv =
('#btnAdd').click(function (e) { e.preventDefault(); var htmlFormatDiv =
(< div id =' uploadContainer' style =' height:30px' > < / div > );
var htmlFormatFile =
("<div id='uploadContainer' style='height:30px'></div>"); var htmlFormatFile =
(< 输入 type =' file' / > 跨度>);
var totalFileCount =
("<input type='file' />"); var totalFileCount =
这篇关于如何为输入类型文件提供验证以接受多个文件的jpg和pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!