1

In My project of mvc where i am consuming i am hitting an api to post data but while posting i am getting an error Because The Model which is in api side contains a property Named File And type of IFile which accepts image so while consuming i dont know how to consume that to send image to an api below is the code for api please help me

 [HttpPost("addService/{Id}")]
    public async Task<IActionResult> AddServices([FromForm]AddServicesDto addserviceDto, int Id)
    {

        var serviceTypeFromRepo = await _repos.GetServiceType(Id);

        var file = addserviceDto.File;

        var uploadResult = new ImageUploadResult();

        if (file.Length > 0)
        {
            using (var stream = file.OpenReadStream())
            {
                var uploadParams = new ImageUploadParams()
                {
                    File = new FileDescription(file.Name, stream),
                    Transformation = new Transformation()
                        .Width(500).Height(500).Crop("fill").Gravity("face")
                };

                uploadResult = _cloudinary.Upload(uploadParams);
            }
        }

        addserviceDto.Url = uploadResult.Uri.ToString();
        addserviceDto.PublicId = uploadResult.PublicId;

        var serviceToCreate = _mapper.Map<Service>(addserviceDto);
        serviceTypeFromRepo.Services.Add(serviceToCreate);
        if (await _repos.SaveAll())
        {
            var serviceToReturn = _mapper.Map<ServiceToReturnDto>(serviceToCreate);
              return Ok(new { serviceToReturn });
        }
        return BadRequest("Could Not add Service");

    }

Above code is controller in api Below is the model

 public class AddServicesDto
{
    public string ServiceName { get; set; }
    public string ServiceDescription { get; set; }
    public string ServiceTitle { get; set; }
    public string Url { get; set; }
    public IFormFile File { get; set; }
    public string PublicId { get; set; }
}

please help me to consume this particular method how can i achieve this using MVC FYI using postman i am able to succeed but i have no idea how to send image via mvc please help me out there

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138

0 Answers0