Search code examples
c#asp.net-corexamarin.formsrefit

How to upload files using NET CORE and Refit


When I use a POSTMAN to do make a request, my API receives a IList<IFormFile>.

Request using POSTMAN

API

How can I do the same request using Xamarin.Forms with REFIT?


Solution

  • You can use IEnumerable<StreamPart> to upload a list of files:

    public interface IApi
    {
        [Multipart]
        [Post("/api/story/{id}/upload-images")]
        Task UploadImages(int id, [AliasAs("files")] IEnumerable<StreamPart> streams);
    }
    

    Then you can call it:

    var api = RestService.For<ISomeApi>("http://localhost:61468");
    var files = new List<StreamPart>()
    {
        new StreamPart(fileStream, "photo.jpg", "image/jpeg"),
        new StreamPart(fileStream2, "photo2.jpg", "image/jpeg")
    };
    
    await api.UploadImages(1, files);