我创建了一个 API,它正在数据库中添加数据,包括值和图像。
在邮递员中,我调用它并在表单数据中设置图像,并在 JSON 中设置原始值,但仍在图像中,我在源代码对象中获取空值。以下是屏幕截图。
我需要两件事上的帮助:
第一个屏幕截图是邮递员,其中我以 json 格式发送数据:
第二个屏幕截图是邮递员,其中我在邮递员中的同一请求中发送图像
现在,在第三张图片中,您可以在图片中看到它显示的是 Null 而不是值。
所以我不明白为什么图像为空。下面是代码
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();
}
Run Code Online (Sandbox Code Playgroud)
[HttpPost]
[Route("CreateUserPreference")]
public async Task<ActionResult> CreateUserPreference([FromBody] UserPreference userPreference)
{
if (ModelState.IsValid)
{
try
{
var userPreferenceId = await …
Run Code Online (Sandbox Code Playgroud)