2

I get a stream form a web service:

Stream resultStream = WebServiceEntities.GetAttachment(attachmentId);

enter image description here

I need to convert this stream into a byte array but

Attempt # 1

byte[] resultBytes = null;
using (Stream stream = resultStream)
{
    using (MemoryStream ms = new MemoryStream())
    {
        int count = 0;
        do
        {
            byte[] buf = new byte[1024];
            count = stream.Read(buf, 0, 1024);
            ms.Write(buf, 0, count);
        } while (stream.CanRead && count > 0);
        resultBytes = ms.ToArray();
    }
}

Attempt # 2:

var memoryStream = new MemoryStream();
resultStream.CopyTo(memoryStream);
byte[] resultBytes = memoryStream.ToArray();

In both cases the (byte[] resultBytes) always returns an empty byte array, does anyone know what is causing this to happen? Is there something wrong with the Stream the web service returns?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Eric Bergman
  • 1,453
  • 11
  • 46
  • 84

0 Answers0