4advanced 23 Junior Poster in Training

Dear reader,

I'm using an IHttpHandler to show content of a PDF saved in a SQL Server 2005 database.
I can't figure out why it keeps showing me "File does not begin with %PDF-"

When I directly write out the bytes to the response it all works fine....

Let me explain a bit more;
After a user has been verified they can browse for pdf documents. If a user isn't verified an image will be created which says the 'filename' can not being requested because of security reasons....


Anybody who can point me to the right direction?

Regards,

Richard
The Netherlands

Some code (simplified)

class BinaryFile
{
   private byte[] _fileData;
   public byte[] FileData
    {
       get {return _fileData;}
       set {_fileData  value;}
    }
}

The FileHandler (simplified)

public ProcessRequest(HttpContext context)
{
 // Checkings for parameters, user etc etc...
 // if all okay, show pdf
  BinaryFile binFile = FileInformation(fileId, includeFileData);
  context.Response.ContentType = "application/pdf";
  context.Response.BinaryWrite(binFile.FileData);
  context.Response.Flush();
  context.Response.Close();
}

private static BinaryFile FileInformation(int id, bool includeFileData)
    {
        BinaryFile binFile = new BinaryFile();
        
        using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(myConnectionString))
        {
            using (System.Data.SqlClient.SqlCommand com = con.CreateCommand())
            {
                com.CommandText = "SELECT ID, [File], Path, Ext, Size, ContentType, Binary FROM BinaryFiles WHERE ID = @ID";
                com.Parameters.AddWithValue("@ID", id);
                try
                {                    
                    con.Open();
                    System.Data.CommandBehavior comBehaviour = 
                        includeFileData == true ? System.Data.CommandBehavior.SequentialAccess : System.Data.CommandBehavior.CloseConnection;

                    using (System.Data.SqlClient.SqlDataReader dr = com.ExecuteReader(comBehaviour))
                    {
                        if (dr.HasRows)
                        {
                            while (dr.Read())
                            {
                                binFile.ID = id;
                                binFile.FileName = dr.IsDBNull(1) ? "" : dr.GetString(1);
                                binFile.Path = dr.IsDBNull(2) ? "" : dr.GetString(2);
                                binFile.Ext = dr.IsDBNull(3) ? "" : dr.GetString(3);
                                binFile.Size = dr.IsDBNull(4) ? 0 : (long)dr.GetValue(4);                                
                                binFile.ContentType = dr.IsDBNull(5) ? "" : dr.GetString(5);
                                                                
                                if (includeFileData)
                                {
                                    int bufferSize = 8040;
                                    byte[] chunk = new byte[bufferSize];
                                    
                                    long retCount;
                                    long startIndex = 0;
                                    retCount = dr.GetBytes(6, startIndex, chunk, 0, bufferSize);

                                    int counter = 0;

                                    byte[] chunkSecond = new byte[bufferSize];
                                    System.Collections.ArrayList arrList = new System.Collections.ArrayList();
                                                                  
                                    while (retCount == bufferSize)
                                    {
                                        startIndex += bufferSize;
                                        retCount = dr.GetBytes(6, startIndex, chunk, 0, bufferSize);
                                        arrList.AddRange(chunk);
                                        counter++;
                                    }

                                    byte[] actualChunk = new byte[retCount - 1];                                   
                                    Buffer.BlockCopy(chunk, 0, actualChunk, 0, (int)retCount - 1);
                                    arrList.AddRange(actualChunk);
                                    // Create the realsize FileData    
                                    binFile.FileData = new byte[arrList.Count];
                                    binFile.FileData = (byte[])arrList.ToArray(typeof(byte));                                    
                                }                                
                            }
                        }
                    }
                }
                catch (System.Data.SqlClient.SqlException exSQL)
                {
                    // My notifications
                }
                catch (Exception ex)
                {
                    // My notifications
                }
                finally
                {
                    con.Close();
                }
            }
        }
        return binFile;        
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.