I am trying to save an image in the web API folder by postman and I received the status code ok,
but the image file does not save . is there any problem with my code?
*ignore save in db part please *
[HttpPost]
public HttpResponseMessage AddItem()
{
string mypath = HttpContext.Current.Server.MapPath("~/images");
if (!Directory.Exists(mypath))
{
Directory.CreateDirectory(mypath);
}
//Fetch the File.
HttpPostedFile postedFile = HttpContext.Current.Request.Files[0];
//Fetch the File Name.
string fileName = Path.GetFileName(postedFile.FileName);
//Save the File.
postedFile.SaveAs(mypath + fileName);
string TfoodType = HttpContext.Current.Request.Form["foodType"];
string TfoodName = HttpContext.Current.Request.Form["foodName"];
int TfoodPrice = Convert.ToInt32(HttpContext.Current.Request.Form["foodPrice"]);
string TfoodDescription = HttpContext.Current.Request.Form["foodDescription"];
//save values in database
using (var context = new ModelFoodDb() )
{
context.fooditem.Add(new FoodItems()
{
// foodType = FM.foodType ,
// foodName = FM.foodName ,
// foodPrice = FM.foodPrice ,
// foodDescription = FM.foodDescription,
// foodImage = FM.foodImage
});
context.SaveChanges();
}
return Request.CreateResponse(HttpStatusCode.OK, fileName);
}