C# 使用文件上载控件在数据库表中存储文件大小

C# 使用文件上载控件在数据库表中存储文件大小,c#,asp.net,sql-server,database,visual-studio-2012,C#,Asp.net,Sql Server,Database,Visual Studio 2012,我需要在使用上载控件上载文件时在数据库表中保存文件大小。 我在这里简单介绍我的上传控制代码。 请看一看 protected void UploadFileControl(object sender, EventArgs e) { string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName); string contentType = FileUpload1.PostedFile.ContentType;

我需要在使用上载控件上载文件时在数据库表中保存文件大小。 我在这里简单介绍我的上传控制代码。 请看一看

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();
                }
            }
        }
    }
}

这是一个简单的上传控制代码。

int length=bytes.length使其可读,以KB、MB、GB为单位,