【发布时间】:2013-04-23 10:39:33
【问题描述】:
这可能吗?我的 ajax 调用如下所示:
$.ajax( {
type: "POST",
url: "/reporter/api/image/getimage/",
data: { "": httpNonAccessibleFilePath },
success: function ( imageData ) {
$( "#imagecontent" ).append( '<img src="' + imageData + '" />' );
}
} );
在我的 Web.API 方面:
[HttpPost]
public HttpResponseMessage GetImage([FromBody]string serverPath)
{
HttpResponseMessage response = new HttpResponseMessage();
if (serverPath != null)
{
FileInfo fileInfo = new FileInfo(serverPath);
if (fileInfo.Exists)
{
response.Content = new StreamContent( fileInfo.OpenRead() );
response.Content.Headers.ContentType = new MediaTypeHeaderValue( "image/png" );
}
}
return response;
}
所有位都连接好,即我可以点击服务并返回数据......但我没有看到任何图像。我在这里错过了什么?
【问题讨论】:
-
你确定 $( "#imagecontent" ) 已经在 DOM 中了吗?
-
忘记这个吧。我忘了看第二段代码。另一个问题是,您的 API 返回什么? URI 还是实际的图像内容?
-
@roasted 是的。我看到损坏的图像图标加载到该图像区域中。如果我只返回一个带有 url 的字符串,指向网络上的一些随机图像,而不是 httpresponsemessage,则图像显示得很好。
-
srcofimg元素必须指向网络浏览器将下载图像的位置。不是服务器将图像内容发送到img。 -
@Nicros 如果 api 返回实际图像,那么为什么首先调用 ajax?
标签: javascript jquery ajax asp.net-web-api