csharp: Gets a files formatted size._.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > csharp: Gets a files formatted size.

csharp: Gets a files formatted size.

 2015/4/20 19:13:25  ?Geovin Du Dream Park?  程序员俱乐部  我要评论(0)
  • 摘要:/*ASP.NET默认上传文件是4M,可以修改服务配置..<system.web><!--指示ASP.NET支持的最大文件上载大小。该限制可用于防止因用户将大量文件传递到该服务器而导致的拒绝服务攻击。指定的大小以KB为单位。默认值为4096KB(4MB)。最大为:2097151KB此处改为40M大小的文件上传限制。--><
  • 标签:file files for ARP
class="brush:csharp;gutter:true;">  /*
            ASP.NET 默认上传文件是4M ,可以修改服务配置.. 

           <system.web>
           <!-- 指示 ASP.NET 支持的最大文件上载大小。
           该限制可用于防止因用户将大量文件传递到该服务器而导致的拒绝服务攻击。
           指定的大小以 KB 为单位。默认值为 4096 KB (4 MB)。最大为:2097151 KB
           此处改为40M大小的文件上传限制。
           -->
           <httpRuntime maxRequestLength = "2097151" useFullyQualifiedRedirectUrl="true"/>
           </system.web>
            */

        /// <summary>
        /// Gets a files formatted size.
        /// </summary>
        /// <param name="file">The file to return size of.</param>
        /// <returns></returns>
        //public static string GetFileSize(this FileInfo file)
        //{
        //    try
        //    {
        //        //determine all file sizes
        //        double sizeinbytes = file.Length;
        //        double sizeinkbytes = Math.Round((sizeinbytes / 1024));
        //        double sizeinmbytes = Math.Round((sizeinkbytes / 1024));
        //        double sizeingbytes = Math.Round((sizeinmbytes / 1024));
        //        if (sizeingbytes > 1)
        //            return string.Format("{0} GB", sizeingbytes); //returns size in gigabytes
        //        else if (sizeinmbytes > 1)
        //            return string.Format("{0} MB", sizeinmbytes); //returns size in megabytes if less than one gigabyte
        //        else if (sizeinkbytes > 1)
        //            return string.Format("{0} KB", sizeinkbytes); //returns size in kilabytes if less than one megabyte
        //        else
        //            return string.Format("{0} B", sizeinbytes); //returns size in bytes if less than one kilabyte
        //    }
        //    catch { return "Error Getting Size"; } //catches any possible error and just returns error getting size
        //}

        /// <summary>
        /// Gets a files formatted size.
        /// 获得文件大小
        /// 涂聚文
        /// 
        /// Geovin Du
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public string GetFileSize(double file)
        {
            try
            {
                //1.
                double byteSize = Math.Round(file / 1024 * 100) * 0.01;
                string suffix = "KB";
                if (byteSize > 1000)
                {
                    byteSize = Math.Round(byteSize * .001 * 100) * .01;
                    suffix = "MB";
                }
                double byteGSize = Math.Round(byteSize / 1024 * 100) * 0.01;
                if (byteGSize > 1000)
                {
                    byteGSize = Math.Round(byteGSize * .001 * 100) * .01;
                    suffix = "GB";
                }
                return byteSize.ToString() + suffix;

                //2.
                //determine all file sizes
                //double sizeinbytes = file;
                //double sizeinkbytes = Math.Round((sizeinbytes / 1024));
                //double sizeinmbytes = Math.Round((sizeinkbytes / 1024));
                //double sizeingbytes = Math.Round((sizeinmbytes / 1024));
                //if (sizeingbytes > 1)
                //    return string.Format("{0:0.00} GB", sizeingbytes); //returns size in gigabytes
                //else if (sizeinmbytes > 1)
                //    return string.Format("{0:0.00} MB", sizeinmbytes); //returns size in megabytes if less than one gigabyte
                //else if (sizeinkbytes > 1)
                //    return string.Format("{0:0.00} KB", sizeinkbytes); //returns size in kilabytes if less than one megabyte
                //else
                //    return string.Format("{0} B", sizeinbytes); //returns size in bytes if less than one kilabyte
            }
            catch 
            { 
                return "Error Getting Size"; 
            } //catches any possible error and just returns error getting size
        }

  

发表评论
用户名: 匿名