-1

I try to Upload a photo from web MVC to API by refit but alway return 400 bad request and data send from client is null These are my codes:

//Refit
[Multipart]
[Post("/api/products/{id}/uploadphoto")]
public Task<ApiResponse<bool>> UploadPhoto(int id,[AliasAs("file")]  StreamPart streamPart);

//Service
   public async Task<bool> UpdaloadPhoto2(int id, IFormFile file)
        {
            var stream = file.OpenReadStream();
            StreamPart streamPart = new StreamPart(stream, file.FileName, file.ContentType, file.Name);
            var res = await productClient.UploadPhoto(id,streamPart);
            if (res.IsSuccessStatusCode)
            {
                return true;
            }
            var content = res.Error.Content;
            AddErrors(content);
            return false;
        }

//Comsume
   [HttpPost("{id}/uploadphoto")]
        public IActionResult UploadPhoto(int id,[FromForm] IFormFile file)
        {
           var res= productService.UploadPhoto(id,file);
           if(res!=null) return Ok();
           return BadRequest(); 
        }
    
yohhhh
  • 1
  • Can you explain what these code parts do? How they are dependent on each other and where do you get 400 error in the code? – Chetan Jul 26 '22 at 02:37
  • StreamPart is a wrapper class of Stream in Refit https://github.com/reactiveui/refit#multipart-uploads – yohhhh Jul 26 '22 at 02:39
  • I use Refit to call API , I try to upload a Photo , wrap it in StreamPart then send to API, but response return 400 , my validation told that "the file is required" – yohhhh Jul 26 '22 at 02:46

1 Answers1

0

The controller needs to receive an IList of IFormFile

Try

public IActionResult UploadPhoto(int id, IList<IFormFile> files)
Rich Bryant
  • 865
  • 10
  • 27