public HttpResponseMessage UploadImage() { // Create new FileInfo object and get the Length. int bucket; string imageName = null; var httpRequest = HttpContext.Current.Request; //upload Image var postedFile = httpRequest.Files["Image"]; var bucketId = httpRequest["BucketId"]; bucket = Int32.Parse(bucketId); //Create custom filename imageName = new String(Path.GetFileNameWithoutExtension(postedFile.FileName).Take(10).ToArray()).Replace(" ", "-"); imageName = imageName + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(postedFile.FileName); //this is where we create the filePath to be saved var filePath = HttpContext.Current.Server.MapPath("~/Image/" + imageName); //and this is where we save the file to backend postedFile.SaveAs(filePath); //Save to DB using (ToshlEntities db = new ToshlEntities()) { product product = new product() { //image caption is the imageCaption from the front end it is the same as we wrote the name for caption name = imageName, size = postedFile.ContentLength, modified = DateTime.Now.ToString(), bucketId = bucket //image name is the filtered name with max 10 letters with added time at the end }; db.products.Add(product); db.SaveChanges(); //} return(Request.CreateResponse(HttpStatusCode.Created, "it works")); } }