Upload file and Check size and type
protected void btnUpload_Click(object sender, EventArgs e)
{
HttpPostedFile file = (HttpPostedFile)(txtFile.PostedFile);
if ((file != null) && (file.ContentLength > 0))
{
if (IsImage(file) == false)
{
// Invalid file type so do something here
return;
}
}
int iFileSize = file.ContentLength;
if (iFileSize > 1000000) // 1MB approx (actually less though)
{
// File is too big so do something here
return;
}
// all good do what you need to do
}
private bool IsImage(HttpPostedFile file)
{
// This checks for image type... you could also do filename extension checks and other things
// but this is just an example to get you on your way
return ((file != null) && System.Text.RegularExpressions.Regex.IsMatch(file.ContentType, "image/\\S+") && (file.ContentLength > 0));
}