Download (FileStreamResult) file endpoint generation
Closed this issue · 9 comments
Tested with 0.6v
I have
[HttpGet("test-file")]
public async Task<FileContentResult> TestFile()
{
var bb = new byte[1];
return File(bb, "application/pdf");
}
when i generate i get
[Get("/test-file")]
Task TestFile();
I expect to get
Task<HttpContent> TestFile();
My idea to be able download file via refit. Maybe i am doing wrong.
Inspired by
https://stackoverflow.com/questions/42141274/refit-c-downloading-image
Yeah, it is caused by this bug: dotnet/aspnetcore#30465.
I can probably add some workaround...
I can see that Refit supports returning Task as well. WDYT, would that be better?
I need to use refit in middle service where i get file via refit and forward it. Returning task wouldnt work in my case.
If it takes much work, then no needed. I will use old but gold pure httpclient ;).
Oh, my bad. I meant returning Stream :)
Oh yea. Stream would be ideal solution.
I have published version 0.7, which has a fix for this. Could you check it?
It generated good result just not sure how refit behaves. Not sure if i will have enough time to check today. If not then only on monday.
It didnt worked, since refit tries deserialize json. I havent checked but it is my assumptions.
I did this.
[HttpGet("test-file")]
[ProducesResponseType(typeof(HttpContent), (int)HttpStatusCode.OK)]
public async Task<FileStreamResult> TestFile()
{
var bb = new byte[1];
return File(bb, "application/pdf");
}
Get this
Task<HttpContent> TestFile();
Then call refit like this and it worked
var data = await _someApi.TestFile();
var stream = await data.ReadAsStreamAsync();
return File(stream, "application/pdf");
So i assume we could close issue
I have created a small Refit test with Stream response, and it seems to work fine. I'm wondering what went wrong for you with Task return type.
using Refit;
var client = RestService.For<IExampleApi>("https://filesamples.com/");
var stream = await client.DownloadFile();
using (var fileStream = File.Create("download.jpg"))
{
stream.CopyTo(fileStream);
}
interface IExampleApi
{
[Get("/samples/image/jpg/sample_640%C3%97426.jpg")]
Task<Stream> DownloadFile();
}
True. This one works too.