Gives solution for any Error occurs in any Programming Language, .Net Core 3.1, 3.0, C#,Asp.Net,Sqlserver,MVC,Java,Php,Html,Css,Jquery,errors and solutions,Latest News,Technology

Monday, 14 March 2016

Restrict to Upload only Attachments/Images only using FileUpload Control in Asp.Net Jquery

No comments :
Here I will explain about suppose sometimes we need a requirement to upload only Attachments like txt,pdf,xls,doc... and image formats like png,jpg,jpeg,bmp,...While in this time we need to give an message to user please upload this type only. Here i can give an alert using bootbox.js jquery plugin.
var validFilesTypes = ["doc", "xls","txt","pdf"];//Here you can add whatever formats you want to use
        function ValidateFile() {
            debugger;
            var file = $('[ID*=fuAttachments]');//File upload control id
            var label = document.getElementById("<%=lblImagesofAsset.ClientID%>");
            var path = file[0].value;
            var ext = path.substring(path.lastIndexOf(".") + 1, path.length).toLowerCase();
            var isValidFile = false;
            for (var i = 0; i < validFilesTypes.length; i++) {
                if (ext == validFilesTypes[i]) {
                    isValidFile = true;
                    break;
                }
            }
            if (!isValidFile) {
                bootbox.alert("Invalid File. Please upload a File with" +
                 " extension:\n\n" + validFilesTypes.join(", "));
                //label.style.color = "red";
                //label.innerHTML = "Invalid File. Please upload a File with" +
                // " extension:\n\n" + validFilesTypes.join(", ");
            }
            return isValidFile;
        }

<asp:Button ID="btnSave" runat="server" BackColor="black" Text="Save" OnClientClick="return ValidateFile();" OnClick="btnSave_Click"/>

No comments :

Post a Comment