i have write this code for upload pdf to Sql DB :

        private void uploadPDFFiles()
            string filetype;
            string filename;

            filename = uploadFilesLinkTextBox.Text.Substring(Convert.ToInt32(uploadFilesLinkTextBox.Text.LastIndexOf("\\")) + 1, uploadFilesLinkTextBox.Text.Length - (Convert.ToInt32(uploadFilesLinkTextBox.Text.LastIndexOf("\\")) + 1));
            filetype = uploadFilesLinkTextBox.Text.Substring(Convert.ToInt32(uploadFilesLinkTextBox.Text.LastIndexOf(".")) + 1, uploadFilesLinkTextBox.Text.Length - (Convert.ToInt32(uploadFilesLinkTextBox.Text.LastIndexOf(".")) + 1));

            MessageBox.Show(filename + " " + filetype);

            //Validate user upload only specific bytes - un comment below lines if you need to validate only PDF files

            if (filetype.ToUpper() != "PDF")
                alertLabel.ForeColor = Color.Red;
                alertLabel.Text = "الرجاء إدخال ملف PDF فقط ...";

                // Open file to read using file path
                FileStream FS = new FileStream(uploadFilesLinkTextBox.Text, System.IO.FileMode.Open, System.IO.FileAccess.Read);

                // Add filestream to binary reader
                BinaryReader BR = new BinaryReader(FS);

                // get total byte length of the file
                long allbytes = new FileInfo(uploadFilesLinkTextBox.Text).Length;

                // read entire file into buffer
                FileBytes = BR.ReadBytes((Int32)allbytes);

                // close all instances
            catch (Exception ex)
                alertLabel.Text = "Error during File Read " + ex.ToString();
                //MessageBox.Show("Error during File Read " + ex.ToString());

inserting all my file bytes inside FileBytes variable and then stored it inside DB

then i download it from db using this code :

        private void DownloadPDFFile(DataRow dr)
            string id;
            FileStream FS = null;
            byte[] dbbyte;

                //Get a stored PDF bytes
                dbbyte = (byte[])dr["UploadFiles"];

                //store file Temporarily 
                string filepath = @"C:\Users\Administrator.minfo-HP\Downloads\NewsAgency.pdf";

                //Assign File path create file
                FS = new FileStream(filepath, System.IO.FileMode.Create);

                //Write bytes to create file
                FS.Write(dbbyte, 0, dbbyte.Length);

                // Open file after write 

                //Create instance for process class
                Process Proc = new Process();

                //assign file path for process
                Proc.StartInfo.FileName = filepath;
            catch(Exception ex)
                throw new System.ArgumentException(ex.Message);
                //Close FileStream instance

when i run this code it give me corrupted pdf downloaded in target folder and does not open, and the file size not the same as original pdf

where is the wrong in my code ???

3 Years
Discussion Span
Last Post by IT_Techno

the field type "Image" but the size write as "16" i do not have choice to change it, can i ?? and how ? iam using Sql Server 2008


Your file is open with the filestream object when you try to open it via Proc.Start();.

At a minimum, add FS.Flush(); after writing out the bytes to flush the buffer to disk. Preferably, close the file before calling Proc.Start();.


I have tried that add FS.Flush() and closed FS.Close(); after write but still as it, when i download pdf file it give me message box say : " The Selected document can not be opened "
why ????


You need to change your field type to 'varbinary' on your database. That should correct your issue.

EDIT: Use varbinary(max) otherwise you will be limited to 8,000 bytes

Edited by Ketsuekiame


I have changed my field type to 'varbinary(max)' but still the same problem :(


i have fixed that problem with small code there is the problem :

at first i have write this :

sqlCMD.Parameters.Add(new SqlParameter("@UploadFiles", 50000,Convert.ToInt32(FileBytes.Length), "UploadFiles")).Value = FileBytes;

the problem in length of 'FileBytes'that inserted to DB to fix that see this code :

sqlCMD.Parameters.Add(new SqlParameter("@UploadFiles", SqlDbType.VarBinary,Convert.ToInt32(FileBytes.Length), "UploadFiles")).Value = FileBytes;

insted off '50000' use 'Convert.ToInt32(FileBytes.Length)' and it's work :)))

thanks for every one .....

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.