ASP.NET Core: Is it possible to use HttpClient to fetch a file and return directly?

烈酒焚心 提交于 2020-06-17 13:20:08

问题


I have an internal API that would fetch and return a fileresult. However, this API does not have any concept of authentication/role/permission checks and cannot be modified to do so.

I would like to create an web API endpoint on an existing ASP.NET Core 2 Web API to do permission check, make a call to this internal API and return the fileresult back to a web client.

Would it be possible to get the wrapper API endpoint to just pass whatever it fetches as a file result without having to reconstruct the response (e.g., specify file name, content type, etc)? The files could be images, pdfs, document. I would prefer that this wrapper API only do permission check and make a call to the internal API endpoint using some sort of fileId and not need to know about the content length or type.


回答1:


Per @chris-pratt's recommendation, turned out it wasn't that complicate to reconstruct the result as I originally anticipated. I ended up implementing this way in case someone needs to do something similar here.

... some validation logic outside the scope of the question...
using (HttpClient client = new HttpClient())
                {
                    var file = await client.GetAsync($"{someURL}/{id}");
                    return new FileContentResult(await file.Content.ReadAsByteArrayAsync(), file.Content.Headers.ContentType.MediaType);
                }


来源:https://stackoverflow.com/questions/49013806/asp-net-core-is-it-possible-to-use-httpclient-to-fetch-a-file-and-return-direct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!