【问题标题】:Calling .NET core REST api from windows forms to send data including IFormFile从 Windows 窗体调用 .NET core REST api 以发送包括 IFormFile 在内的数据
【发布时间】:2019-03-15 06:10:06
【问题描述】:

我有一个使用此模型的 .NET 核心 Web Api:

public class UserForRegisterDto
{
    [Required]
    public string UserName { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string LastName { get; set; }
    [Required]      
    public string Password { get; set; }
    [Required]
    public string PhoneNumber { get; set; }
    [Required]
    public string MobilePhone { get; set; }

    [Required]
    public IFormFile Photo { get; set; }
}

另一方面,我有一个使用 .NET 4 的 windows 窗体应用程序,我尝试使用此类和一段代码将数据发送到 api:

public class UserForRegisterDto
{
    public string UserName { get; set; }
    public string Name { get; set; }
    public string LastName { get; set; }
    public string Password { get; set; }
    public string PhoneNumber { get; set; }
    public string MobilePhone { get; set; }
    public byte[] Photo { get; set; }
}

发送此数据的代码是:

List<UserForRegisterDto> registrationList = Mapper.Map<List<UserForRegisterDto>>(usersList);
AuthenticatedHttpClientHandler clientHandler = new AuthenticatedHttpClientHandler(user.Id, Uow, _refreshTokenService);
clientHandler.UseDefaultCredentials = true;
HttpClient client = new HttpClient(clientHandler);
client.BaseAddress = new Uri(ConfigurationManager.AppSettings["appUrl"].ToString());
StringContent content = new StringContent(JsonConvert.SerializeObject(registrationList), Encoding.UTF8, "application/json");


HttpResponseMessage response = await client.PostAsync($"api/users/{user.Id}/users/PostGasStationUsers/{ConfigurationManager.AppSettings["appId"].ToString()}", content);

我需要使用另一种数据类型而不是可以映射到 IFormFile 数据的字节数组以及如何做到这一点。

【问题讨论】:

    标签: c# asp.net-core


    【解决方案1】:

    使用MultipartFormDataContent传输二进制数据

    var content = new MultipartFormDataContent();
    
    for (int i = 0; i < registrationList.Count; i++)
    {
        UserForRegisterDto registration = registrationList[i];
    
        content.Add(new StringContent(registration.LastName), $"model[{i}].LastName");
        content.Add(new StringContent(registration.MobilePhone), $"model[{i}].MobilePhone");
        content.Add(new StringContent(registration.Name), $"model[{i}].Name");
        content.Add(new StringContent(registration.Password), $"model[{i}].Password");
        content.Add(new StringContent(registration.PhoneNumber), $"model[{i}].PhoneNumber");
        content.Add(new StringContent(registration.UserName), $"model[{i}].UserName");
        content.Add(new ByteArrayContent(registration.Photo), $"model[{i}].Photo", "photo.jpeg");
    }
    
    HttpResponseMessage response = await client.PostAsync($"api/users/{user.Id}/users/PostGasStationUsers/{ConfigurationManager.AppSettings["appId"].ToString()}", content);
    

    动作参数名称为model时有效

    [HttpPost]
    public IActionResult PostGasStationUsers(List<UserForRegisterDto> model)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-30
      • 1970-01-01
      • 2019-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-21
      相关资源
      最近更新 更多