amin mohammadi
amin mohammadi

Reputation: 1053

Calling .NET core REST api from windows forms to send data including IFormFile

I have a .NET core Web Api with this model:

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; }
}

On the other side I have a windows forms application using .NET 4 and I try to send data to the api using this class and piece of code:

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; }
}

and the piece of code to send this data is:

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);

I need to use another data type instead of byte array which can be mapped to IFormFile data and how to do that.

Upvotes: 0

Views: 805

Answers (1)

Alexander
Alexander

Reputation: 9632

Use MultipartFormDataContent to transfer binary data

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);

It works if action parameter name is model

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

Upvotes: 1

Related Questions