我使用Asp.net
c#
<asp:FileUpload ID="FileUpload2" multiple="multiple" class="form-control" runat="server" />
我想在上传时验证客户端文件的选择必须是6.
答案 0 :(得分:3)
function ValidateFile2(){
var fileCount = document.getElementById('FileUpload2').files.length;
if(fileCount > 10) // Selected images with in 10 count
{
alert("Please select only 10 images..!!!");
return false;
}
else if(fileCount <= 0) // Selected atleast 1 image check
{
alert("Please select atleat 1 image..!!!");
return false;
}
return true; // Good to go
}
答案 1 :(得分:1)
尝试以下
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<form id="form1" runat="server">
<div>
<input type="file" ID="fuPhoto" multiple="multiple" />
<input type="button" ID="btnUpload" Text="Upload" Enabled="false" />
<label ID="myLabel" ForeColor="#CC0000" />
</div>
</form>
<script>
$(function () {
$('#fuPhoto').change(
function () {
var files = $('input[type="file"]')[0].files;
alert(files.length )
//var fileExtension = ['jpeg', 'jpg'];
if (files.length>6) {
$('#btnUpload').attr("disabled", true);
$('#myLabel').html("limit upto six");
}
else {
$('#btnUpload').attr("disabled", false);
$('#myLabel').html(" ");
}
})
})
</script>
更新了以下代码
$(function () {
var files = $('input[type="file"]')[0].files;
alert(files.length )
if (files.length>6) {
$('#btnUpload').attr("disabled", true);
alert("limit upto six");
}
else {
//nothing
}
})
答案 2 :(得分:1)
&lt; asp:FileUpload ID =“FileUpload2” ClientIDMode =“静态”multiple =“multiple”runat =“server”/&gt;
JQuery解决方案:
$(document).ready(function () {
$('#FileUpload2').change(function () {
var files = $(this)[0].files;
if (files.length != 6) {
alert("Six files have to be selected at a time!");
}
else
{
submit();//your custom method to submit the form
}
});
});
注意:我可以使用ID作为选择器,因为我已将ClientIDMode属性设置为static。此属性是从.NET 4.0 [Click here to know more]引入的。或者,您也可以将控件的类名用作选择器。
答案 3 :(得分:0)
您可以使用后面的代码并执行类似
的操作 if(FileUpload2.Count < 6)
{
//Error
}
else
{
//OK
}
您还可以在页面上使用一些JavaScript来进行验证
`
function Validate() {
var f = document.getElementById("fu");
if(f.files.length < 6)
{
}
else
{
}
}
</script>`
并点击按钮事件集OnClientClick="Validate();"
答案 4 :(得分:0)
在后面的代码中尝试下面,
if (FileUpload1.PostedFiles.Count > 2)
{
//error will show if file number more than 2
}
else
{
//will proceed with uploading if file count not more than 2
}