Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8442259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:48:13+00:00 2026-06-10T08:48:13+00:00

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

  • 0

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

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

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

<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:

GQ8XQAYFAiEMfN0qD0COTgMX

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

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:48:15+00:00Added an answer on June 10, 2026 at 8:48 am

    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.

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

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

    Your json result would then be something like…

    {
        "TestMethodResult":"GQ8XQAYFAiEMfN0qD0COTgMX"
    }
    

    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

    Option 2 return a stream:

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WCF service method that returns a big byte[] array around 2
I have a very simple WCF service running that has a single method that
i have a simple xml file in a wcf service that i am trying
I have a simple REST web service in WCF that is declared as follows:
I've been trying to do a simple restful wcf service that will return JSON.
I have a simple WCF service which has a method that returns a complex
We have a simple WCF service that is tagged with InstanceContextMode = Single and
I have written a simple WCF service that accepts and stores messages. It works
I have written a very simple WCF service, that worked fine (code below), then
I have a WCF service hosted that is returning a byte[] of protobuf-net serialized

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.