<td> <span class="inpFileWrap"> <input type="file" id="customerPic" name="customerPic" tabindex="51" class="inputFile" önchange="PreviewCustomerImg(this)" /></span> <label id="label" class="note"> Select an image file on your computer(100kb max):</label> <label id="imageError" class="error marginTm25 dispNone"> </label> @if (Session["customerpic"] != null) { byte[] temp23 = Session["customerpic"] as byte[]; <span class="imgExistPrev"> <img style="border:2 ,0,#FFFFFF;padding:50" width="60" height="80" src="data:image/jpg;base64,@(Convert.ToBase64String(temp23))" alt="" /> </span> } <div id="customerPicPreview"> </div> </td> <style type="text/css"> #customerPicPreview { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale); } </style> JQuery function - function PreviewCustomerImg(imgFile) { $.ajax({ type: 'POST', cache: false, url: '/AddCustomer/VerifyImageSize?CustomerImage=' + imgFile.value, success: function (data) { var value = data; if (value == 1) { //TODO: Shanker needs to take care of this $('#imageError').show().html("Only .jpg , .jpeg, .png files can be uploaded.").removeClass("dispNone"); $("#customerPicPreview").hide(); return false; } if (value == 2) { //TODO: Shanker needs to take care of this $('#imageError').show().html("File size should not exceed more than 100KB").removeClass("dispNone"); ; $("#customerPicPreview").hide(); return false; } else { $('#imageError').html("").hide(); $("#customerPicPreview").show(); var newPreview = document.getElementById("customerPicPreview"); newPreview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgFile.value; newPreview.style.width = "100px"; newPreview.style.height = "100px"; } } }); } My Controller Code is - [HandleError(ExceptionType = typeof(Exception), View = "ApplicationError")] public JsonResult VerifyImageSize(string CustomerImage) { HttpPostedFileBase photo = Request.Files["customerPic"]; string filenameWithPath = CustomerImage; string filename = System.IO.Path.GetFileName(filenameWithPath); string e = Path.GetExtension(CustomerImage); if (e == null) { return new JsonResult { Data = 1 }; } string path = Server.MapPath("~/App_Data"); byte[] imgData = System.IO.File.ReadAllBytes(CustomerImage); //Check file extention here if (e == ".jpeg" || e == ".jpg" || e == ".png") { if (imgData.Length > 102400) { return new JsonResult { Data = 2 }; } else { return new JsonResult { Data = 3 }; } } else { return new JsonResult { Data = 1 }; } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)