Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 455
Matheus Velloso Avatar asked Dec 19 '22 01:12

Matheus Velloso


1 Answers

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);
like image 163
Alex Riabov Avatar answered Dec 29 '22 16:12

Alex Riabov