3

In my web application, I have a connection to a web service. In my Web Service, it has a method for getting the bytes of a report, based on the path:

public byte[] GetDocument(string path)
{
   byte[] bytes = System.IO.File.ReadAllBytes(path);
   return bytes;
}

Now, when I make a request from my web application, the web service gives me a Json object, similar to this:

{
  "Report":"0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAA
   AAACAAAA3QAAAAAAAAAAEAAA3wAAAAEAAAD+ ... (continues)" 
}

And in my web application I have a method for fetching the Json object, putting the "report" in a string. The web application then has a method for parsing the string of bytes into an array of bytes, which does not work, it throws a FormatException:

public byte[] DownloadReport(string id, string fileName)
{
    var fileAsString = _api.DownloadReport(id, fileName);
    byte[] report = fileAsString.Split()
                                .Select(t => byte.Parse(t, NumberStyles.AllowHexSpecifier))
                                .ToArray();
    return report;
}

I have also tried to do this:

public byte[] DownloadReport(string id, string fileName)
{
   var fileAsString = _api.DownloadReport(id, fileName);
   byte[] report = Encoding.ASCII.GetBytes(fileAsString);
   return report;
}

Which gave me a .doc file with the exact same string as the Json object had.

Am I parsing something the wrong way from the web service, or is it when I want to convert it to an array of bytes again?

3
  • Please add the whole Exception you are getting Commented Nov 19, 2013 at 10:23
  • 1
    @Serv, it was nput string was not in a correct format.. , and then a bunch of paths. but @Dmytro Rudenko's answer did the thing. Commented Nov 19, 2013 at 10:28
  • 1
    I already saw, that Dmytro's answer helped you. Commented Nov 19, 2013 at 10:29

1 Answer 1

8
public byte[] DownloadReport(string id, string fileName)
    {
        var fileAsString = _api.DownloadReport(id, fileName);
        byte[] report = Convert.FromBase64String(fileAsString);
        return report;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank god / thank you! I thought I had tried this, but obvious I had not. Works like a charm now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.