如何返回使用JSON一个WCF REST服务Base64编码的字节数组? [英] How to return a Base64 encoded byte array from a WCF REST service using JSON?

查看:173
本文介绍了如何返回使用JSON一个WCF REST服务Base64编码的字节数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的WCF REST方法,将一个字节数组返回图像/文件/ etc:

I have a simple WCF REST method that will return an image/file/etc in a byte array:

[OperationContract]
[WebGet(UriTemplate = "TestMethod")]
byte[] TestMethod();



服务合同绑定到的WebHttpBinding 具有下列行为:

<endpointBehaviors>
  <behavior name="webHttpBehavior">
    <webHttp defaultOutgoingResponseFormat="Json" />
  </behavior>
</endpointBehaviors>



该方法工作正常,但该字节数组的格式如下:

The method works fine, except the byte array is formatted like:

[25,15,23,64,6,5,2,33,12,124,221,42,15,64,142,78,3,23]

如果我删除属性 defaultOutgoingResponseFormat =Json的,该服务默认为XML格式,结果以Base64编码,如:

If I remove the attribute defaultOutgoingResponseFormat="Json", the service defaults to XML formatting, and the result is encoded in Base64 like:

GQ8XQAYFAiEMfN0qD0COTgMX

从而节省了数据传送,特别是当数据获取大

which saves on data transfer, especially when the data get large.

如何我启用了JSON输出格式Base64编码?

How can I enable Base64 encoding for the JSON output format?

推荐答案

我面对我们公司的网络服务类似的问题在几个月前。我必须弄清楚如何使用JSON端点发送一个字节数组。不幸的是,没有一个简单的答案。我发现了两个变通不过,我决定去与一个最简单的。我会让你决定如果这两个是有帮助的。

I faced a similar issue with our company's web service a few months ago. I had to figure out how to send a byte array using json endpoints. Unfortunately there isn't an easy answer. I found two work arounds however and I decided to go with the easiest one. I'll let you decide if either of these are helpful.

选项1返回base64编码字符串,而不是一个字节数组:

Option 1 return a base64 encoded string instead of a byte array:

微软的转换库轻松地转换成字节数组基地64串,反之亦然。

Microsoft's Convert library easily converts a byte array to a base 64 string and vice versa.

[OperationContract]
[WebGet(UriTemplate = "TestMethod")]
string TestMethod();

public string TestMethod()
{
    byte[] data = GetData();
    return Convert.ToBase64String(data);
}

您JSON结果然后将类似...

Your json result would then be something like...

{
    "TestMethodResult":"GQ8XQAYFAiEMfN0qD0COTgMX"
}

那么你的客户端可以将其转换回一个字节数组。如果客户端还使用C#的那么容易,因为

Then your client can convert that back to a byte array. If the client is also using C# its as easy as

byte[] data = Convert.FromBase64String("GQ8XQAYFAiEMfN0qD0COTgMX");

如果你有一个相当大的字节数组但是,因为是在我们的例子,下面可能是一个更好的选择。

If you have an rather large byte array however, as was in our case, the following might be a better option

选项2返程流:

是的,这意味着你将不会得到JSON。你基本上只是发送原始数据和设置内容头让客户知道如何解释它。这个工作对我们,因为我们是简单地发送到浏览器的图像。

Yes this does mean that you will not be getting json. You are essentially just sending raw data and setting the content header so the client knows how to interpret it. This worked for us because we were simply sending an image to a browser.

[OperationContract]
[WebGet(UriTemplate = "TestMethod")]
Stream TestMethod();

public Stream TestMethod()
{
    byte[] data = GetData();
    MemoryStream stream = new MemoryStream(data);
    WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg"; //or whatever your mime type is
    stream.Position = 0;
    return stream;
}

这篇关于如何返回使用JSON一个WCF REST服务Base64编码的字节数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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