Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I've selected binary data by its name from database then I showed a SaveFileDialog to save that binary data somewhere on my computer. I wrote the following code to do that but when I opened saved file, there is not any data in it.It just work correctly for txt file. I understood that the problem is in following line:

byte[] content = sdr["Content"] as byte[];


IN following line for any size of file it just can read 200byte of it .for example i uploaded the file with 367byte, It uploaded correctly but when i downloaded it, in follwing line, content is 200byte.

private void btnDownload_Click(object sender, EventArgs e)
     {
          try
         {
                    using (SqlCommand cmd = new SqlCommand("SelectFile", Conn))
                 {
                     cmd.CommandType = CommandType.StoredProcedure;
                     Conn.Open();
                     cmd.Parameters.Add("@FileName", SqlDbType.VarChar).Value = "1.txt";
                     string Ext = Path.GetExtension("1.txt");
                     SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                     if (sdr.HasRows)
                     {
                         sdr.Read();
                         byte[] content =  sdr["Content"] as byte[] ;
                         if (fdDownload.ShowDialog() == DialogResult.OK)
                         {
                             FileStream fs = new FileStream(fdDownload.FileName + Ext  , FileMode.CreateNew, FileAccess.Write);
                         fs.Write(content, 0, content.Length);
                         fs.Flush();
                         fs.Close();
                         }
                         lblMessage.ForeColor = System.Drawing.Color.Green;
                         lblMessage.Text = "File  Downloaded Successfully";
                         Conn.Close();
                     }
                     else
                     {
                         lblMessage.ForeColor = System.Drawing.Color.Green;
                         lblMessage.Text = "File  not Downloaded Successfully";
                         Conn.Close();
                     }
                 }


         }
         catch (SqlException sqlEx)
         {
             lblMessage.ForeColor = System.Drawing.Color.Red;
             lblMessage.Text = "File not  Downloaded Successfully";
         }

     }
Posted
Updated 17-Jun-14 0:53am
v2

1 solution

Hello friend,
please visit the following article as this may be helpful to your requirement:

Upload and Download Files with SQL Server in ASP.NET[^]

- DD
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900