title: Stream Excel File for Download after getting Excel from WCF Service
body: <p>Got a problem here, and once again think I am close to a solution, however it is eluding me.</p>
  
  <p>I am trying to download an Excel file from our ASP.NET website after getting the filestream from a WCF service. This seems to work okay, but I may be going the entirely wrong way about it.</p>
  
  <p>Using breakpoints in the code I am able to see that the file is arriving in the method and being "streamed" to the browser, however it is not becoming available for download (when the little icon appears in the corner, or a prompt appears, depending on browser).</p>
  
  <p>I've tried to adapt this code <a href="http://www.codeproject.com/Articles/166763/WCF-Streaming-Upload-Download-Files-Over-HTTP" rel="nofollow">http://www.codeproject.com/Articles/166763/WCF-Streaming-Upload-Download-Files-Over-HTTP</a> to accept .xls files.</p>
  
  <p>Here is my FileTransfer.cs and IFileTransfer.cs WCF classes.
  FileTransfer.cs</p>
  
  <pre><code>public RemoteFileData DownloadFile(DownloadRequest request)
              {
                  RemoteFileData result = new RemoteFileData();
                  try
                  {
                      string filePath = System.IO.Path.Combine(@"C:\Files", request.FileName);
                      System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
  
                      // does file exist?
                      if (!fileInfo.Exists)
                          throw new System.IO.FileNotFoundException("File not found", request.FileName);
  
                      // open the stream
                      System.IO.FileStream stream = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
  
                      // return result over the stream
                      result.FileName = request.FileName;
                      result.Length = fileInfo.Length;
                      result.FileByteStream = stream;
                  }
                  catch (Exception ex)
                  {
                  }
                  return result;
              }
  </code></pre>
  
  <p>IFileTransfer.cs</p>
  
  <pre><code>[ServiceContract]
  
  public interface IFileTransfer
  {
      [OperationContract]
      RemoteFileData DownloadFile(DownloadRequest request);
  }
  
  [MessageContract]
  public class DownloadRequest
  {
      [MessageBodyMember]
      public string FileName;
  }
  
  [MessageContract]
  public class RemoteFileData : IDisposable
  {
      [MessageHeader(MustUnderstand = true)]
      public string FileName;
  
      [MessageHeader(MustUnderstand = true)]
      public long Length;
  
      [MessageBodyMember(Order = 1)]
      public System.IO.Stream FileByteStream;
  
      public void Dispose()
      {
          if (FileByteStream != null)
          {
              FileByteStream.Close();
              FileByteStream = null;
          }
      }
  }
  </code></pre>
  
  <p>And here is the Linkbutton method/event.</p>
  
  <pre><code>protected void LnkBtn_genPAFFile_Click(object sender, EventArgs e)
  {
      try
      {
          GSIISFileTransferService.IFileTransfer tClient = new GSIISFileTransferService.FileTransferClient();
          GSIISFileTransferService.DownloadRequest requestData = new GSIISFileTransferService.DownloadRequest();
          GSIISFileTransferService.RemoteFileData fileInfo = new GSIISFileTransferService.RemoteFileData();
  
          requestData.FileName = "form.xls";
          fileInfo = tClient.DownloadFile(requestData);
  
          Response.BufferOutput = false;
          byte[] buffer = new byte[6500];
          int bytesRead = 0;
  
          HttpContext.Current.Response.Clear();
          HttpContext.Current.Response.ClearHeaders();
          HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
          HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + requestData.FileName);
  
          bytesRead = fileInfo.FileByteStream.Read(buffer, 0, buffer.Length);
  
          while (bytesRead &gt; 0)
          {
              if (Response.IsClientConnected)
              {
                  Response.OutputStream.Write(buffer, 0, bytesRead);
                  // flush data to HTML
                  Response.Flush();
  
                  buffer = new byte[6500];
                  bytesRead = fileInfo.FileByteStream.Read(buffer, 0, buffer.Length);
              }
              else
              {
                  bytesRead = -1;
              }
          }
      }
      catch (Exception ex)
      {
          System.Web.HttpContext.Current.Response.Write("Error : " + ex.Message);
      }
      finally
      {
          Response.Flush();
          Response.Close();
          Response.End();
          System.Web.HttpContext.Current.Response.Close();
      }
  }
  </code></pre>