1

I need to save a file size in database table while uploading a file in using upload control. i am simple pitching my upload control code here. Please take a look.

protected void UploadFileControl(object sender, EventArgs e)
{
    string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
    string contentType = FileUpload1.PostedFile.ContentType;
    using (Stream fs = FileUpload1.PostedFile.InputStream)
    {
        using (BinaryReader br = new BinaryReader(fs))
        {
            byte[] bytes = br.ReadBytes((Int32)fs.Length);
            using (SqlConnection con = new SqlConnection(con))
            {
                string str = "INSERT INTO XYZ VALUES (@FileName, @ContentType, @Data)";
                using (SqlCommand cmd = new SqlCommand(str))
                {
                    cmd.Connection = con;
                    cmd.Parameters.AddWithValue("@FileName", fileName);
                    cmd.Parameters.AddWithValue("@ContentType", contentType);
                    cmd.Parameters.AddWithValue("@Data", bytes);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }
    }
}

This is simple upload control code.

Anurag Dixit
  • 27
  • 1
  • 1
  • 11

0 Answers0