简体   繁体   中英

Invalid Character in base64 string when decoding XML

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM