URL에서 이미지를 다운로드하여 컨트롤러를 통해 제공하고 싶습니다. 이상적으로는 바이트 배열 등을 만들려고 시도하는 것보다 가능한 한 많이 스트리밍하고 싶습니다.
foo.ImageUrl
소비자 에게 제공 하는 것이 아니라 컨트롤러를 통해 파일을 다운로드하는 이유 는 URL이 http이고 혼합 콘텐츠를 피하기 위해 프록시를 사용하기 때문입니다. 이 작업은 절대 URL을 html img
태그 에 넣어 호출됩니다.
이것이 내가 지금까지 가지고있는 것입니다.
문제는 이미지에 내용이없는 것 같습니다. img
태그는 빈보고 내 브라우저에서로 이동하면 난 그냥 다운로드 헤더와 아무것도 시도를 참조?
URL에서 다운로드하여 호출자에게 스트림으로 반환하려면 어떻게해야합니까?
[HttpGet]
[AllowAnonymous]
[Route(template: "Reward/FooLogo/{fooId}/bar/{barId}", Name = "FooLogo")]
public async Task<StreamContent> FooLogo(int fooId, int barId)
{
var foo = await GetFooAsync(fooId, barId);
if (string.IsNullOrWhiteSpace(foo?.ImageUrl))
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
using (var response = await WebRequest.Create(foo.ImageUrl).GetResponseAsync())
{
// todo check content type
var responseStream = response.GetResponseStream();
var content = new StreamContent(responseStream);
content.Headers.ContentType = new MediaTypeHeaderValue(response.ContentType);
content.Headers.ContentLength = response.ContentLength;
return content;
}
}
제거하는 것만으로 using
는 문제가 해결되지 않는 것 같습니다. 나는 그것을 조금 다시 썼고 이것이 내 문제를 해결 한 것 같습니다.
[HttpGet]
[AllowAnonymous]
[Route(template: "Reward/FooLogo/{fooId}/bar/{barId}", Name = "FooLogo")]
public async Task<HttpResponseMessage> FooLogo(int fooId, int barId)
{
var foo = await GetFooAsync(fooId, barId);
if (string.IsNullOrWhiteSpace(foo?.ImageUrl))
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
using (var client = new HttpClient())
{
var res = await client.GetAsync(paymentMethod.ImageUrl);
var response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StreamContent(await res.Content.ReadAsStreamAsync());
response.Content.Headers.ContentType = res.Content.Headers.ContentType;
return response;
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다