2017-05-03 48 views
0

我在我的Asp.Net MVC项目中使用HttpHandler。我有另一个MVC API项目返回图像作为回应。使用HttpWebRequest我可以调用API,代码中没有错误,但我无法查看页面中的图像。获取图像作为HttpWebRequest在HttpHandler中的响应

我的代码:

的HttpHandler代码:

var currentResponse = HttpContext.Current.Response; 

string URL = "http://localhost:50417/API/GetThumbnail"; 
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL); 
        request.KeepAlive = false; 
        request.ProtocolVersion = HttpVersion.Version10; 
        request.Method = "GET"; 
        request.Timeout = 30000; 
        HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

        StreamReader streamr = new StreamReader(response.GetResponseStream()); 


        currentResponse.Write(streamr.ReadToEnd()); 

RouteConfig.cs

routes.Add(new Route("Thumbnail/getImage", new ThumbnailImageRouteHandler())); 

index.csHtml

<img src="/Thumbnail/getImage" /> 
+0

您是否尝试过指定contenttype?如果您在浏览器中打开网址,您是否可以下载/查看图像? – Fixation

+0

@Fixation是我可以下载图片,如果我在浏览器中打开网址,我也试过contenttype – Jigarb1992

回答

1

这工作如果你设置了ContentType prope并简单地将响应流复制到输出中,如下所示:

response.GetResponseStream().CopyTo(currentResponse.OutputStream); 
currentResponse.ContentType = response.ContentType; 
+0

谢谢,但不工作@Johnny – Jigarb1992

+0

它为我工作时,我用你的例子中的最后两行代替。您是否在Visual Studio或您的Web浏览器控制台中遇到任何错误? –

+0

反对!对不起,我再次重新检查一遍,我给出的路径是错误的。它的工作@Johnny谢谢:) – Jigarb1992

相关问题