public string PostUserImage(int id) { try { var httpRequest = HttpContext.Current.Request; foreach (string file in httpRequest.Files) { HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created); var postedFile = httpRequest.Files[file]; if (postedFile != null && postedFile.ContentLength > 0) { IList <string> AllowedFileExtensions = new List <string> { ".jpg", ".gif", ".png" }; var ext = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf('.')); var extension = ext.ToLower(); if (!AllowedFileExtensions.Contains(extension)) { var message = string.Format("Please Upload image of type .jpg,.gif,.png."); return("Invalid File Extension"); } else { var filePath = HttpContext.Current.Server.MapPath("~/Images/" + postedFile.FileName + extension); postedFile.SaveAs(filePath); //Save to DB using (gladiatorEntities db = new gladiatorEntities()) { image image = new image() { imgCaption = httpRequest["ImageCaption"], imgName = postedFile.FileName, UID = id }; db.images.Add(image); db.SaveChanges(); } } } var message1 = string.Format("Image Updated Successfully."); return(message1); } var res = string.Format("Please Upload a image."); return(res); } catch (Exception ex) { var res = string.Format(ex.ToString()); return(res); } }
public IHttpActionResult Caption(image newimg) { gladiatorEntities dbi = new gladiatorEntities(); var captionRow = dbi.images.FirstOrDefault(x => x.UID == newimg.UID); //var crop_current = db.crops.FirstOrDefault(u => u.CID == approval.CID); //var bidder = db.users.FirstOrDefault(u => u.UID == crop_current.UID); if (captionRow != null) { captionRow.imgCaption = newimg.imgCaption; //crop_current.current_bid = approval.bid1; //crop_current.bidder_name = bidder.full_name; dbi.SaveChanges(); } return(Ok()); }