C#asp.net FileUpload 获取文件大小KB MB G

    private const double KBCount = 1024;
    private const double MBCount = KBCount * 1024;
    private const double GBCount = MBCount * 1024;
    private const double TBCount = GBCount * 1024;

    public static string GetAutoSizeString(double size, int roundCount)
    {
        if (KBCount > size)
        {
            return Math.Round(size, roundCount) + "B";
        }
        else if (MBCount > size)
        {
            return Math.Round(size / KBCount, roundCount) + "KB";
        }
        else if (GBCount > size)
        {
            return Math.Round(size / MBCount, roundCount) + "MB";
        }
        else if (TBCount > size)
        {
            return Math.Round(size / GBCount, roundCount) + "GB";
        }
        else
        {
            return Math.Round(size / TBCount, roundCount) + "TB";
        }

    }









调用代码:



 Label2.Text = GetAutoSizeString(Convert.ToDouble("这里就是字节数") , 0); ///  后面这个 0  就是说 取整数, 1的话 就是保留一位小数  





运用实例:


       文件大小.Text= Convert.ToString(FileUpload1.PostedFile.ContentLength);
       文件大小.Text = GetAutoSizeString(Convert.ToDouble(文件大小.Text) ,2);









webform
桂ICP备16004754号