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 ???

Recommended Answers

All 8 Replies

What is the field type and length in your database?

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

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

how can i know if i upload my pdf to my DB correctly ?

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 .....

Be a part of the DaniWeb community

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