Set Max number of files to be uploaded in ASP.Net Multiple FileUpload control

Last Reply one year ago By nagaraju60

Posted one year ago

hello,

i have asp dot net fileupload control in which i have enable multi file selection. i want to apply limitation so user can select max 3 images not more than that

please advice

Posted one year ago

Hi nauna,

Please refer below sample.

HTML

<input id="FileUpload1" onchange="Validate()" type="file" multiple="multiple" />
<script type="text/javascript">
    function Validate() {
        var fileCount = document.getElementById("FileUpload1").files.length;
        if (fileCount > 4) {
            alert("max 3 images allowed.");
            document.getElementById("FileUpload1").value = null;
        }
    }
</script>

Demo


Posted one year ago

Dear @nauna,

Please refer below example , it might help you

        <asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="true" />
        <br />
        <asp:RequiredFieldValidator ErrorMessage="Required" ControlToValidate="FileUpload1"  runat="server" Display="Dynamic" ForeColor="Red" />
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" ValidationExpression="(.*?)\.(jpg|jpeg|png|gif|JPG|JPEG|PNG|GIF)$" ControlToValidate="FileUpload1" runat="server" ForeColor="Red" ErrorMessage="Please select a valid image file." Display="Dynamic" />

        <asp:CustomValidator ID="CustomValidator1" runat="server" ForeColor="Red" ErrorMessage="CustomValidator" ControlToValidate="FileUpload1" ClientValidationFunction="ValidateFileLimit"></asp:CustomValidator>


        <br />
        <asp:Button Text="Submit" runat="server" />
<script type="text/javascript">  
    function ValidateFileLimit(sender, args) {
        var fileCount = document.getElementById('FileUpload1').files.length;
        if (fileCount > 3) // Selected images with in 3 count
        {
            alert("Please select only 3 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
    }
</script>