首页 > 解决方案 > Web服务文件流到字节[]到文件流到pdf失败C#

问题描述

我正在尝试创建一个将文件web service返回为 a的应用程序,然后使用它的应用程序抓取并将其保存为文件,然后打开它。该文件最终无法打开。pdfbyte[]byte[]pdf

这是返回一个byte[]

[WebMethod]
public byte[] XXXX(int fileID)
{
    try
    {
        using (EntitiesModel dbContext = new EntitiesModel())
        {   
            string fileFullPath = .....
            .......     
            if (fileFullNamePath != null)
            {
                FileStream fileStream = new FileStream(fileFullNamePath, FileMode.Open, System.IO.FileAccess.Read);
                int len = fileStream.Length.ToInt();
                Byte[] documentContents = new byte[len];
                fileStream.Read(documentContents, 0, len);
                fileStream.Close();
                return documentContents;

然后使用以下代码从应用程序调用它

string soap = "<?xml version=\"1.0\" encoding=\"utf - 8\"?>" +
              "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
              "<soap:Body>" +
              "<XXXX xmlns=\"http://tempuri.org/\">" +
              "<fileID>XXXXX</fileID>" +
              "</XXXX>" +
              "</soap:Body>" +
              "</soap:Envelope>";
string localhostContext = @"http://localhost:3381/";
string webserviceAddress = @"XXXX/XXXX/XXXXX.asmx";
string url = localhostContext + webserviceAddress ;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "text/xml";
request.ContentLength = soap.Length;
request.Timeout = 20000;
request.Method = "POST";
using (Stream stream = request.GetRequestStream())
{
    using (StreamWriter streamWriter = new StreamWriter(stream))
    {
        streamWriter.Write(soap);               }
    }
}
byte[] bytes;
try
{
    WebResponse response = request.GetResponse();
    bytes = ReadFully(response.GetResponseStream());
}
catch (Exception exception)
{
    throw;
}

private byte[] ReadFully(Stream input)
{
    byte[] buffer = new byte[16*1024];
    using (MemoryStream memoryStream = new MemoryStream())
    {
        int read;
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            memoryStream.Position = 0;
            memoryStream.Write(buffer, 0, read);
        }
        return memoryStream.ToArray();
    }
}

FileStream objfilestream =
                new FileStream(fileName, FileMode.Create,FileAccess.ReadWrite);
objfilestream.Write(bytes, 0, bytes.Length);
objfilestream.Close();
var process = Process.Start(fileName);

代码运行良好并创建一个pdf然后尝试打开它pdf。但是文件无法打开。Adobe Acrobat 给出错误

Adobe Acrobat Reader could not open XXX.pdf because it is either not a 
supported file type or because the file has been damaged (for example, it 
was sent as an email attachment and wasn't correctly decoded).

因为我没有在代码中收到错误,所以我不知道错误在哪里,没有创建正确的文件。

Stream调用的变量有一个问题input没有给出长度,所以我Jon Skeet's在这里使用了建议Stackoverflow:Creating a byte array from a stream

new byte[16*1024]; 

而不是

new byte[input.length]

标签: c#arraysweb-servicespdf

解决方案


错了三件事。

memoryStream.Position = 0;

在 while 循环中有问题,所以我将其删除。

其次,在阅读流时。它返回的是带有XXXXResult标签SOAP XMl中编码字符串的消息。所以我不得不提取它。base64XML

最后我不得不使用

byte[] fileResultBytes  = Convert.FromBase64String(resultString);

从消息中提取的 resultString 中获取 byte[] SOAPSOAP在可以本地生成的测试消息中,它会告诉您此结果字符串的类型。我最初错过了这一点。

感谢他们VC.OneCodeCaster正确建议。


推荐阅读