Image Uploading In MVC WebAPI

  1. [Route("user/PostUserImage")]    
  2.     
  3.     public async Task<HttpResponseMessage> PostUserImage()    
  4.     {    
  5.         Dictionary<stringobject> dict = new Dictionary<stringobject>();    
  6.         try    
  7.         {     
  8.     
  9.         var httpRequest = HttpContext.Current.Request;    
  10.     
  11.         foreach (string file in httpRequest.Files)    
  12.         {    
  13.             HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created);    
  14.     
  15.             var postedFile = httpRequest.Files[file];    
  16.             if (postedFile != null && postedFile.ContentLength > 0)    
  17.             {    
  18.     
  19.                 int MaxContentLength = 1024 * 1024 * 1; //Size = 1 MB    
  20.     
  21.                 IList<string> AllowedFileExtensions = new List<string> { ".jpg"".gif"".png" };    
  22.                 var ext = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf('.'));    
  23.                 var extension = ext.ToLower();    
  24.                 if (!AllowedFileExtensions.Contains(extension))    
  25.                 {    
  26.     
  27.                     var message = string.Format("Please Upload image of type .jpg,.gif,.png.");    
  28.     
  29.                     dict.Add("error", message);    
  30.                     return Request.CreateResponse(HttpStatusCode.BadRequest, dict);    
  31.                 }    
  32.                 else if (postedFile.ContentLength > MaxContentLength)    
  33.                 {    
  34.     
  35.                     var message = string.Format("Please Upload a file upto 1 mb.");    
  36.     
  37.                     dict.Add("error", message);    
  38.                     return Request.CreateResponse(HttpStatusCode.BadRequest, dict);    
  39.                 }    
  40.                 else    
  41.                 {    
  42.     
  43.                     YourModelProperty.imageurl = userInfo.email_id + extension;    
  44.                 //  where you want to attach your imageurl    
  45.     
  46.                   //if needed write the code to update the table    
  47.     
  48.                     var filePath = HttpContext.Current.Server.MapPath("~/Userimage/" + userInfo.email_id+extension);    
  49.                        //Userimage myfolder name where i want to save my image    
  50.                     postedFile.SaveAs(filePath);    
  51.     
  52.                 }    
  53.             }    
  54.     
  55.             var message1 = string.Format("Image Updated Successfully.");    
  56.             return Request.CreateErrorResponse(HttpStatusCode.Created, message1); ;    
  57.         }    
  58.         var res = string.Format("Please Upload a image.");    
  59.         dict.Add("error", res);    
  60.         return Request.CreateResponse(HttpStatusCode.NotFound, dict);    
  61.         }    
  62.         catch(Exception ex)    
  63.         {    
  64.             var res = string.Format("some Message");    
  65.             dict.Add("error", res);    
  66.             return Request.CreateResponse(HttpStatusCode.NotFound, dict);    
  67.         }    
  68.     }