I have a controller
[HttpPost] public ActionResult ChangeAvatar(HttpPostedFileBase file) { AvatarHelper.AvatarUpdate(file, User.Identity.Name); return RedirectToAction("Index", "Profile"); }
And I already check if the file is in jpeg / png format:
private static bool IsImage(string contentType) { return AllowedFormats.Any(format => contentType.EndsWith(format, StringComparison.OrdinalIgnoreCase)); } public static List<string> AllowedFormats { get { return new List<string>() {".jpg", ".png", ".jpeg"}; } }
What I need is to ensure that the downloaded file is a real image file, not a txt file with an image extension.
I convert my downloaded file as follows:
using (var image = System.Drawing.Image.FromStream(postedFile.InputStream)) {
I am thinking of a try / catch block to create an image from an input stream, but I am wondering if it has a good way? Thanks)
PS
I wonder if there is another (more efficient way that tries to / catch the block) to check if the file is a real way?
source share