Matheus Velloso
Matheus Velloso

Reputation: 175

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?

Upvotes: 5

Views: 10525

Answers (1)

Alex Riabov
Alex Riabov

Reputation: 9175

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

Upvotes: 17

Related Questions