title: Web service File Stream to Byte[] to FileStream to pdf failing C#
body: <p>I am trying to create a <code>web service</code> that returns a <code>pdf</code> file as a <code>byte[]</code> and then the app that consumes it grabs the <code>byte[]</code> and saves it as a <code>pdf</code> file and then opens it. The file fails to open at the end.</p>
  
  <p>Here is the <strong>Web Service</strong> that returns a <code>byte[]</code></p>
  
  <pre class="lang-cs prettyprint-override"><code>[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;
  </code></pre>
  
  <p>Then it is called from an <strong>app</strong> with the following code</p>
  
  <pre class="lang-cs prettyprint-override"><code>string soap = "&lt;?xml version=\"1.0\" encoding=\"utf - 8\"?&gt;" +
                "&lt;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/\"&gt;" +
                "&lt;soap:Body&gt;" +
                "&lt;XXXX xmlns=\"http://tempuri.org/\"&gt;" +
                "&lt;fileID&gt;XXXXX&lt;/fileID&gt;" +
                "&lt;/XXXX&gt;" +
                "&lt;/soap:Body&gt;" +
                "&lt;/soap:Envelope&gt;";
  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)) &gt; 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);
  </code></pre>
  
  <p>The code runs fine and creates a <code>pdf</code> and then tries to open that <code>pdf</code>. But the file can not be opened. Adobe Acrobat gives the error </p>
  
  <pre><code>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).
  </code></pre>
  
  <p>Because I am not getting an error in the code I am at a loss to know where the error is that is not creating the proper file.</p>
  
  <p>There was an issue with the <code>Stream</code> variable called <code>input</code> was not giving length so I used <code>Jon Skeet's</code> suggestion here <a href="https://stackoverflow.com/questions/221925/creating-a-byte-array-from-a-stream">Stackoverflow:Creating a byte array from a stream</a></p>
  
  <pre><code>new byte[16*1024]; 
  </code></pre>
  
  <p>rather than </p>
  
  <pre><code>new byte[input.length]
  </code></pre>