简体   繁体   English

解码XML时base64字符串中的无效字符

[英]Invalid Character in base64 string when decoding XML

We have a Winform client app that is comsuming a web service we write. 我们有一个Winform客户端应用程序,正在使用我们编写的Web服务。 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. 此客户端应用程序请求XML文件中包含的文档,通常是将XML写入XML文件中以base64编码的二进制字段中的PDF。

Client successfully downloads, decodes, and opens 99% of the documents correctly. 客户端正确地成功下载,解码并打开了99%的文档。

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- 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. 我们已经将base64 blob从XML文件写到了文本文件。 I don't see any "\\0" characters. 我没有看到任何“ \\ 0”字符。 I could post the whole blob, but it's quite large. 我可以发布整个Blob,但是它很大。

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. 我们对每个块进行了base64编码。 WRONG. 错误。

Resolution- Write all the chunks to a global memorystream object. 解决方法-将所有块写入全局内存流对象。 At the end of the callbacks, then do the base64 encoding. 在回调的末尾,然后执行base64编码。

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. msbase64Out是全局内存流,每次调用回调时都会写入该内存流。 viewdocWriter is a global XML writer that is responsible for writing out the XML stream that gets sent to the client app. viewdocWriter是全局XML编写器,负责写出发送到客户端应用程序的XML流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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