i am giving a field to user where he can upload any
image file i want to check that the file should not
be more large then 350kb …. how to do this in c#
HttpPostedFileBase file = Request.Files[0];
string mynewpath = Request.PhysicalApplicationPath + "Upload\\";
if (file.ContentLength > 0)
{
// here i want to check that if file size is more then 350kb then i will give error
string[] extarray = new string[] { "image/jpeg", "image/jpg","image/png", "image/gif" };
var isallowedfile = extarray.Contains(file.ContentType);
if (!isallowedfile)
{
ModelState.AddModelError("", "Only image files (.jpeg , .gif , .png ) are accepted, please browse a image file");
return View("SurveyOptions", model);
}
string filename = Guid.NewGuid() + Path.GetFileName(file.FileName);
file.SaveAs(mynewpath + filename);
}
In older browsers its not possible to get file size before uploading it,
workaround for this is to embed hidden
flash(actionscript)element to get the file sizeIn latest browesers you can use
HTML5 File APIto read file sizecheck jquery filedrop plugin (https://github.com/weixiyen/jquery-filedrop) which is based on
HTML5 file API–NJ