Search Asp.Net Here

Check File Type while uploading in Asp.Net using JavaScript




Here is an example of we can check the file type that is being uploaded by the user or your web page in Asp.Net.
I m using Java Script to check the file type. If the file type is correct than it will be uploaded to the server other wise an alert will be given to user to upload a valid file type.

Use Following code in your .aspx page

<asp:fileupload id="FileUpload1" runat="server"></asp:fileupload>
<asp:button id="Button1" onclientclick="Check_file_type()" runat="server" text="Upload"> </asp:button>


Use following javascript function in your script file or between opening or closing of script tag in your page.

function Check_file_type()
{
var uploadedFile = document.getElementById('FileUpload1');
if (uploadedFile.value =='' )
{
alert("Select a file for Uploading!");
uploadedFile.focus();
return false;
}
var filetype = new Array(".doc", ".docx");
var extension = uploadedFile.value.slice(uploadedFile.value.indexOf(".")).toLowerCase();
for (var i = 0; i < filetype.length; i++) { if (filetype[i] == extension) return true; } alert("Upload correct file type i.e.:\n\n"+filetype.join("\n")); uploadedFile.focus(); return false; }

Try it...




Comments :

0 comments to “Check File Type while uploading in Asp.Net using JavaScript”

Post a Comment