title: Working with FileTable and ASP.NET C# (Open a PDF)
body: <p>I am trying to open a PDF in the browser (or download) PDF's when a user clicks on a link. I found a similar question on SO (<a href="https://stackoverflow.com/questions/13970617/opening-filetable-files-in-c-sharp-net-4">Opening FileTable Files in c# / .net 4</a>) and was trying to implement the answer in my code, but have not had any luck. I really just need to see a complete example of how to open a file from a FileTable in ASP.NET / C#.</p>
  
  <p>Code:</p>
  
  <pre><code>public FileResult Download(int? id)
  {
      //start of my test code
      Document document = db.Documents.Find(id);
      string fileName;
      byte[] fileData;
      System.Guid path = document.DocumentPath;
  
      using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["FT_ConnectionString"].ConnectionString))
      {
          string sql = "SELECT TOP 1 file_stream, name, file_type, cached_file_size FROM FTAdvisoryOpinions WHERE stream_id = @SID";
  
          using (var command = new SqlCommand(sql, connection))
          {
              command.Parameters.Add("@SID", SqlDbType.UniqueIdentifier).Value = path;
              connection.Open();
              using (var reader = command.ExecuteReader())
              {
                  if (reader.Read())
                  {
                      //file_stream = 0
                      //name = 1
                      //file_type = 2
                      //cached_file_size = 3
  
                      long fileSize = reader.GetInt64(3); //think this might be wrong
                      string contentType = reader.GetString(2);
  
  
                      fileData = reader.GetBytes(0, 2, fileData, 0, fileSize);  //says I have some invalid arguments
                      fileName = reader.GetString(1);
                      Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
  
                      return File(fileData, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
                  }
                  else
                  {
                      connection.Close();
                  }
              }
  
              return null; // just returning null here til I figure it out.
          }
      }
  }
  </code></pre>