从REST风格的WCF服务返回的图像 [英] Returning an image from a RESTful WCF service

查看:131
本文介绍了从REST风格的WCF服务返回的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个非常简单的DTO:

  [DataContract] 
公共类DTO
{
〔数据成员]
公众的byte []图片{搞定;组; }
}

和这个非常简单的服务:

  [的ServiceContract] 
公共接口IFooService
{
[WebGet(
UriTemplate =,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
名单,LT; DTO> GetDTOs();
}

在我的Global.asax,我有:

  RouteTable.Routes.Add(新ServiceRoute(富,
新WebServiceHostFactory()的typeof(FooService接口)));

现在,当我把这个从我的浏览器,我得到JSON格式的字节数组。好为止。现在,我怎么把字节数组转换为图像?



或者,是否有要对这个更好的办法?我试图改变字节[] ,但后来当我打电话从Firefox服务,响应是空的,尽管200.我使用Firebug小提琴手HTTP状态代码。



我不认为这是相关的,但由于太多的信息不会伤害任何人谁不是机器人,这里的web.config中:<?/ p>

 < XML版本=1.0> 
<结构>
< system.serviceModel>
<&行为GT;
< serviceBehaviors>
<&行为GT;
< serviceMetadata httpGetEnabled =真/>
< serviceDebug includeExceptionDetailInFaults =FALSE/>
< /行为>
< / serviceBehaviors>
< /行为>
< serviceHostingEnvironment multipleSiteBindingsEnabled =真aspNetCompatibilityEnabled =真/>
< /system.serviceModel>
< system.webServer>
<模块runAllManagedModulesForAllRequests =真正的>
<添加名称=UrlRoutingModuleTYPE =System.Web.Routing.UrlRoutingModule,System.Web程序,版本= 4.0.0.0,文化=中性公钥= b03f5f7f11d50a3a/>
< /模块>
< /system.webServer>
<&的System.Web GT;
<编译调试=真targetFramework =4.0/>
< /system.web>
< /结构>



最后,我想问题是:你是怎么从一个WCF REST风格的服务,这样的JavaScript返回一个位图在浏览器中运行可以把它在屏幕上?


解决方案

好为止。现在,我怎么把字节数组转换为图像?




既然你已经标记使用javascript你的问题我认为这是你用什么来消费服务,并要显示在浏览器中的图像。如果是这样的话,你可以看看在数据URI方案这样可以让你展现。图像给你从服务中获得的字节



下面是一个例子,你可以如何使用jQuery消费等服务,并显示图像:

  $(函数(){
$ .getJSON(/富/',函数(DTOS){
$。每次(DTOS ,功能(索引,DTO){
无功海峡= String.fromCharCode.apply(字符串,dto.Image);
$('< IMG />',{
ALT: '',
SRC:数据:图像/ PNG; BASE64,'+ $ .base64.encode(STR)
})appendTo('身体');
});
});
});



$。base64.encode 函数来从这个插件


I have this very simple DTO:

[DataContract]
public class DTO
{
    [DataMember]
    public byte[] Image {get; set; }
}

And this very simple service:

[ServiceContract]
public interface IFooService
{
    [WebGet(
        UriTemplate = "", 
        RequestFormat = WebMessageFormat.Json, 
        ResponseFormat = WebMessageFormat.Json)]
    List<DTO> GetDTOs();
}

In my global.asax, I have:

RouteTable.Routes.Add(new ServiceRoute("foo", 
    new WebServiceHostFactory(), typeof(FooService)));

Now, when I call this from my browser, I am getting an array of bytes in JSON format. Good so far. Now, how do I turn that array of bytes into an image?

Or, is there a better way of going about this? I tried changing byte[] to Stream, but then when I call the service from Firefox, the response is empty despite an HTTP status code of 200. I am using Firebug and Fiddler.

I don't think it's relevant, but since too much information never hurt anybody who wasn't a robot, here's the web.config:

<?xml version="1.0"?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior>
                    <serviceMetadata httpGetEnabled="true"/>
                    <serviceDebug includeExceptionDetailInFaults="false"/>
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
    </system.serviceModel>
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true">
            <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        </modules>
    </system.webServer>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
</configuration>

Ultimately, I guess the question is: how do you return a bitmap from a WCF RESTful service so JavaScript running in the browser can throw it up on the screen?

解决方案

Good so far. Now, how do I turn that array of bytes into an image?

Since you have tagged your question with javascript I assume that this is what you use to consume the service and you want to display the image in the browser. If this is the case you may take a look at the Data URI scheme which would allow you to show the image given the bytes you get from the service.

Here's an example how you could consume such service using jQuery and show the image:

$(function () {
    $.getJSON('/foo/', function (dtos) {
        $.each(dtos, function (index, dto) {
            var str = String.fromCharCode.apply(String, dto.Image);
            $('<img/>', {
                alt: '',
                src: 'data:image/png;base64,' + $.base64.encode(str)
            }).appendTo('body');
        });
    });
});

The $.base64.encode function comes from this plugin.

这篇关于从REST风格的WCF服务返回的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆