How to Check Uploaded File Extension – ASP .Net
If you have fileUpload as your upload control
<input id="fileUpload" type="file" runat="server" />
This code sample check the file type if its an image. It can be altered for other file types as well. This example code is in C#.
HttpPostedFile myPostedFile=fileUpload.PostedFile;
if(myPostedFile!=null && myPostedFile.ContentLength>0)
{
FileInfo finfo = new FileInfo(myPostedFile.FileName);
string fileExtension = finfo.Extension.ToLower();
if (fileExtension != ".gif" && fileExtension != ".jpg" && fileExtension != ".jpeg" && fileExtension != ".png")
{
//show error message
return;
}
}
From Webcosmo Webmaster Forum http://www.webcosmoforums.com/asp/21239-how-check-uploaded-file-extension-asp-net.html











too good!!
when I changed the extension of a .pdf file to .jpg (but the file is not really a .jpg) this function does not return any error.
Is there any way that does not look at the extension just writing in the full file name?
Leave your response!
Categories
Most Commented
Recent Comments
Blogroll
Most Viewed