I have created one API and it is adding data in database, with values and image.
In postman I am calling that and setting image in form-data and other values in raw in JSON but still in image I am getting null value in source code object. Below are screenshots.
I want help in two things :
- Why I am getting Null in image value in source code, please check below image.
- How can I send both image and data using postman, check below screenshots is that ok what I have tried ?
First screenshot is of postman in which I am sending data in json format :
Second screenshot is of postman in which I am sending image in same request in postman
Now in third image you can see in Image it is showing Null instead of values.
So I am not getting why image is coming null..Below is code for this
public async Task<int> Create(UserPreference _object)
{
if(_object.Image != null)
{
var webRootPath = hostingEnv.WebRootPath;
var fileName = Path.GetFileName(_object.Image.FileName);
var filePath = Path.Combine(hostingEnv.WebRootPath, "images\\User", fileName);
using(var fileStream = new FileStream(filePath, FileMode.Create))
{
await _object.Image.CopyToAsync(fileStream);
}
_object.ImagePath = filePath;
}
var obj = await applicationDbContext.UserPreferences.AddAsync(_object);
return applicationDbContext.SaveChanges();
}
[HttpPost]
[Route("CreateUserPreference")]
public async Task<ActionResult> CreateUserPreference([FromBody] UserPreference userPreference)
{
if (ModelState.IsValid)
{
try
{
var userPreferenceId = await _userPreference.Create(userPreference);
if (userPreferenceId > 0)
return Ok("User Preference has added");
}
catch(Exception)
{
return BadRequest();
}
}
return BadRequest();
}
[Display(Name = "Name")]
public string Name { get; set; } = "";
[Display(Name = "Location")]
public string Location { get; set; } = "";
[Display(Name = "Date of Birth")]
public DateTime DateOfBirth { get; set; }
[Display(Name = "About Me")]
public string AboutMe { get; set; } = "";
[Display(Name = "Match Preference")]
public string MatchPreference { get; set; } = "";
[Display(Name = "Play Score")]
public string PlayScore { get; set; } = "";
[Display(Name = "Location")]
public string LocationPreference { get; set; } = "";
[Display(Name = "CreatedOn"), DataType(DataType.Date)]
public DateTime CreatedOn { get; set; } = DateTime.Now;
[Display(Name ="Upload Photo")]
public string ImagePath { get; set; } = "";
[NotMapped]
public IFormFile Image { get; set; }
[ForeignKey("User")]
public int UserId { get; set; }
public virtual User User { get; set; }







