2

We have a Winform client app that is comsuming a web service we write. This client app requests documents that are contained in XML files, generally a PDF written to a base64 encoded binary field in the XML file.

Client successfully downloads, decodes, and opens 99% of the documents correctly.

However, we've started encountering some files that are failing when the client makes this call:

 byte[] buffer = Convert.FromBase64String(xNode["fileIMAGE"].InnerText);

System.FormatException-

Message="Invalid character in a Base-64 string."
Source="mscorlib"

We've written out the base64 blob from the XML file to a text file. I don't see any "\0" characters. I could post the whole blob, but it's quite large.

Any ideas?

6
  • Any chance you could add some code to report all characters that aren't in isalnum(), +, or /? That ought to leave a big pile of newlines or crlf or similar, padding at the end, and your mystery character. Commented May 7, 2011 at 2:03
  • I'm sitting here writing a test app to parse over the resulting chars to test that very thing. Trying to implement the solution offered here stackoverflow.com/questions/3355407/… Commented May 7, 2011 at 2:09
  • There are = (equal signs) in the body of the base64 blob. I don't think that's a valid base64 character (A-Z a-z 0-9 + /) other than padding at the end. Commented May 7, 2011 at 3:13
  • @paparush, hrm, that = in the middle sounds troublesome. Can you try your encoding routines on small inputs? Generate random byte strings between ten bytes and ten kilobytes, maybe you'll generate one by happy accident that shows the problem. Commented May 7, 2011 at 4:00
  • 1
    Is it possible that the blob is multiple Base64 objects, each ending in the = padding? Commented May 7, 2011 at 14:47

1 Answer 1

1

Issue Resolved

To stream the file from the server, we use a callback function to read/write chunks of the file. We were base64encoding each chunk. WRONG.

Resolution- Write all the chunks to a global memorystream object. At the end of the callbacks, then do the base64 encoding.

In the callback function:

 if (brData.ChunkNo == 1)
    {

        // Set the Content-type of the file
        if (brData.MimeType.Length < 1)
        {
            mimeType = "application/unknown";
        }
        else
        {
            mimeType = brData.MimeType;
        }

        msbase64Out = new MemoryStream();
    }


    if (brData.bytesJustRead > 0)
    {
        fileMS.WriteTo(msbase64Out);

    }


   if (brData.bytesRemaining < 1)
    {
        byte[] imgBytes = msbase64Out.ToArray();

        string img64 = Convert.ToBase64String(imgBytes);

        viewdocWriter.WriteString(img64);
    }

msbase64Out is a global memory stream that gets written to each time the callback is called. viewdocWriter is a global XML writer that is responsible for writing out the XML stream that gets sent to the client app.

Sign up to request clarification or add additional context in comments.

Comments

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.