Monday, 14 March 2016
Restrict to Upload only Attachments/Images only using FileUpload Control in Asp.Net Jquery
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.
<asp:Button ID="btnSave" runat="server" BackColor="black" Text="Save" OnClientClick="return ValidateFile();" OnClick="btnSave_Click"/>
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;
}
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment