小编Nic*_*Nic的帖子

Postman 发送 json 和图像数据

我创建了一个 API,它正在数据库中添加数据,包括值和图像。

在邮递员中,我调用它并在表单数据中设置图像,并在 JSON 中设置原始值,但仍在图像中,我在源代码对象中获取空值。以下是屏幕截图。

我需要两件事上的帮助:

  1. 为什么我的源代码中的图像值为空,请检查下图。
  2. 如何使用邮递员发送图像和数据,检查下面的屏幕截图是否可以我尝试过的?

第一个屏幕截图是邮递员,其中我以 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)

c# asp.net-web-api postman .net-core asp.net-core

5
推荐指数
2
解决办法
1万
查看次数

标签 统计

.net-core ×1

asp.net-core ×1

asp.net-web-api ×1

c# ×1

postman ×1