Web服务文件流到Byte []到FileStream到pdf失败的C#

时间:2019-05-21 06:09:31

标签: c# arrays web-services pdf

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

这是 Web服务,它返回一个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。但是文件无法打开。 Adobe Acrobat给出错误

pdf

因为我没有在代码中遇到错误,所以我很茫然地知道错误在哪里并没有创建正确的文件。

名为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). 的{​​{1}}变量没有给出长度,因此我在Stackoverflow:Creating a byte array from a stream这里使用了Stream的建议

input

而不是

Jon Skeet's

1 个答案:

答案 0 :(得分:0)

有三件事错了。

memoryStream.Position = 0;

在while循环中出现问题,因此我将其删除。

第二,当读取流时。它返回的是SOAP XMl消息,该消息在XXXXResult base64标记中带有编码的XML字符串。所以我必须提取它。

最后我不得不使用

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

SOAP消息中提取的resultString中获取字节[]。在可以在本地生成的测试SOAP消息中,它告诉您此结果字符串的类型。我最初很想念那个。

感谢VC.OneCodeCaster的正确建议。