I want store image in sql server I am using dot net web api, at the add method i get NUllReferenceException how to solve??
I am a begginer in webapi
This is the Code i have written
public async Task Upload(IFormFile file) { try {
var uploadsFolderPath=Path.Combine(host.WebRootPath, "uploads");
if (!Directory.Exists(uploadsFolderPath))
Directory.CreateDirectory(uploadsFolderPath);
var fileName= Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
var filePath = Path.Combine(uploadsFolderPath, fileName);
using (var stream = new FileStream(filePath, FileMode.Create))
{
await file.CopyToAsync(stream);
}
var photo = new Photo { FileName = fileName };
photoContext.vehiclePhoto.Add(photo); // NullReferenceException
await photoContext.SaveChangesAsync();
return Ok(photo);
}
catch
{
return BadRequest();
}
Aucun commentaire:
Enregistrer un commentaire