邮递员发送 json 中的数据和图像
Postman send both data in json and image
我创建了一个 API,它正在向数据库中添加数据,包括值和图像。
在邮递员中,我调用它并在表单数据中设置图像,在 JSON 中设置原始值,但在图像中,我在源代码对象中得到空值。以下是屏幕截图。
我需要两件事的帮助:
- 为什么我在源代码中得到的图片值为空,请查看下图。
- 如何使用 postman 发送图像和数据,请检查下面的屏幕截图是否可以?
第一个屏幕截图是邮递员的截图,我在其中以 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();
}
[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; }
您不能同时发送表单数据和 JSON 正文
要解决您的问题,请发送图像和您的其他信息作为表格日期
更多详情见截图
要使用 Postman 发送图像和数据,您必须使用表单数据内容类型。
Select 正文 => 表单数据。列键有一个 selector。对于文件 select 文件类型和 select 文件。要同时发送数据,您必须在键值对中分隔 json。
并从动作中删除 [FromBody] 属性。它仅用于 Content-Type: application/json,但你的 Content-Type: multipart/form-data;
Postman 不允许您像那样同时发送两种不同的表单类型。您的问题是发送 JSON 有效负载 或 图像的二进制内容。
因此,将文件 和 JSON 有效载荷添加到 form-data 屏幕,如下所示:
Postman 会自动设置 Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
header,然后像这样处理实际的 body:
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="image"; filename="/C:/Users/xyz/Downloads/hystrix-dashboard-netflix-api-example-iPad.png"
Content-Type: image/png
(data)
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="data"
我创建了一个 API,它正在向数据库中添加数据,包括值和图像。
在邮递员中,我调用它并在表单数据中设置图像,在 JSON 中设置原始值,但在图像中,我在源代码对象中得到空值。以下是屏幕截图。
我需要两件事的帮助:
- 为什么我在源代码中得到的图片值为空,请查看下图。
- 如何使用 postman 发送图像和数据,请检查下面的屏幕截图是否可以?
第一个屏幕截图是邮递员的截图,我在其中以 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();
}
[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; }
要使用 Postman 发送图像和数据,您必须使用表单数据内容类型。
Select 正文 => 表单数据。列键有一个 selector。对于文件 select 文件类型和 select 文件。要同时发送数据,您必须在键值对中分隔 json。
并从动作中删除 [FromBody] 属性。它仅用于 Content-Type: application/json,但你的 Content-Type: multipart/form-data;
Postman 不允许您像那样同时发送两种不同的表单类型。您的问题是发送 JSON 有效负载 或 图像的二进制内容。
因此,将文件 和 JSON 有效载荷添加到 form-data 屏幕,如下所示:
Postman 会自动设置 Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
header,然后像这样处理实际的 body:
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="image"; filename="/C:/Users/xyz/Downloads/hystrix-dashboard-netflix-api-example-iPad.png"
Content-Type: image/png
(data)
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="data"