public async Task <JsonResult> Image() { try { var company = Request.Form["path"]; var fileName = "file0"; foreach (string file in Request.Files) { var fileContent = Request.Files[file]; if (fileContent != null && fileContent.ContentLength > 0) { //before uploading image make sure it does not exist in path // get a stream var stream = fileContent.InputStream; // and optionally write the file to disk var path = Path.Combine(Server.MapPath("~/UPLOADS/" + company), fileName + ".png"); var index = 0; while (true) { if (!UploadUtility.IsExist(path)) { break; } else { index++; fileName = "file" + index; path = Path.Combine(Server.MapPath("~/UPLOADS/" + company), fileName + ".png"); } } using (var fileStream = System.IO.File.Create(path)) { stream.CopyTo(fileStream); } } } return(Json(new { success = true, data = "UPLOADS/" + company + "/" + fileName + ".png" })); } catch { return(Json(new { success = false, message = MessageUtility.ServerError() })); } }