如何使用JSON从WCF REST服务返回Base64编码的字节数组?

时间:2021-12-03 08:23:31

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

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

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

The service contract is bound to a webHttpBinding with the following behavior:

服务契约绑定到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]

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

如果我删除defaultOutgoingResponseFormat="Json"属性,服务默认为XML格式,结果编码为Base64,如下所示:

GQ8XQAYFAiEMfN0qD0COTgMX

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

这节省了数据传输,尤其是当数据变大时。

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

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

1 个解决方案

#1


9  

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.

几个月前,我在公司的web服务上遇到了类似的问题。我必须弄清楚如何使用json端点发送字节数组。不幸的是,没有一个简单的答案。但是我发现有两个变通的办法,于是我决定采用最简单的办法。我让你决定这两种方法是否有用。

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

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

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

微软的转换库很容易将字节数组转换为基64字符串,反之亦然。

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

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

Your json result would then be something like...

然后,您的json结果将类似于……

{
    "TestMethodResult":"GQ8XQAYFAiEMfN0qD0COTgMX"
}

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

然后您的客户端可以将其转换回字节数组。如果客户也在使用c#,那就简单多了

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

但是,如果您有一个相当大的字节数组,就像我们的例子一样,下面的选项可能是更好的选择

Option 2 return a stream:

选项2返回流:

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.

是的,这确实意味着您不会得到json。本质上,您只是发送原始数据并设置内容头,以便客户端知道如何解释它。这对我们很有用,因为我们只是将一个图像发送到浏览器。

[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;
}

#1


9  

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.

几个月前,我在公司的web服务上遇到了类似的问题。我必须弄清楚如何使用json端点发送字节数组。不幸的是,没有一个简单的答案。但是我发现有两个变通的办法,于是我决定采用最简单的办法。我让你决定这两种方法是否有用。

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

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

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

微软的转换库很容易将字节数组转换为基64字符串,反之亦然。

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

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

Your json result would then be something like...

然后,您的json结果将类似于……

{
    "TestMethodResult":"GQ8XQAYFAiEMfN0qD0COTgMX"
}

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

然后您的客户端可以将其转换回字节数组。如果客户也在使用c#,那就简单多了

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

但是,如果您有一个相当大的字节数组,就像我们的例子一样,下面的选项可能是更好的选择

Option 2 return a stream:

选项2返回流:

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.

是的,这确实意味着您不会得到json。本质上,您只是发送原始数据并设置内容头,以便客户端知道如何解释它。这对我们很有用,因为我们只是将一个图像发送到浏览器。

[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;
}