Utils.cs
上传用户:whjkdz
上传日期:2013-05-19
资源大小:79k
文件大小:6k
- #region License
- /*
- * SunriseUpload - Asp.net Upload Component
- *
- * Copyright (C) 2004 mic <mic4free@hotmail.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- * In case your copy of SunriseUpload does not include a copy of the license, you may find it online at
- * http://www.gnu.org/copyleft/gpl.html
- *
- * You can find new release of this component at http://athena.9966.org/SunriseUpload .
- */
- #endregion
- using System;
- using System.IO;
- using System.Text;
- using System.Web;
- namespace Sunrise.Web.Upload
- {
- internal class Utils
- {
- #region Fields
- private static string version;
- private static string RESOURCE_FILE_PREFIX = "Sunrise.Web.Upload.Resources.";
- #endregion
- #region Constructors
- static Utils()
- {
- }
- public Utils()
- {
- }
- #endregion
- /// <summary>
- /// return the current httpcontext object.
- /// </summary>
- /// <returns></returns>
- public static HttpContext GetContext()
- {
- HttpContext context = HttpContext.Current;
- if (context == null)
- {
- throw new Exception("HttpContext not found");
- }
- return context;
- }
- /// <summary>
- /// return the current version of assembly.
- /// </summary>
- /// <returns></returns>
- public static string GetVersion()
- {
- if (Utils.version == null)
- {
- int majorVersion = typeof (Utils).Assembly.GetName().Version.Major;
- Utils.version = majorVersion.ToString();
- }
- return Utils.version;
- }
- /// <summary>
- /// Return the path of upload folder.
- /// </summary>
- /// <returns></returns>
- public static string GetUploadFolder()
- {
- string uploadFolder = GetContext().Request["Sunrise_Web_Upload_UploadFolder"];
- //If upload folder deos not exist, use system temporary folder to hold the file.
- if ((uploadFolder == null) || (uploadFolder == string.Empty))
- {
- uploadFolder = Path.GetTempPath();
- }
- return uploadFolder;
- }
- /// <summary>
- /// Load file from chache.
- /// </summary>
- /// <param name="filename"></param>
- /// <returns></returns>
- private static byte[] LoadFileFromCache(string filename)
- {
- byte[] buffer;
- HttpContext context = GetContext();;
-
- if(context.Cache[RESOURCE_FILE_PREFIX + filename] == null)
- {
- context.Cache[RESOURCE_FILE_PREFIX + filename] = LoadAssemblyFiles(filename);
- }
-
- buffer = (byte[])context.Cache[RESOURCE_FILE_PREFIX + filename];
- return buffer;
- }
- /// <summary>
- /// Load file from assembly.
- /// </summary>
- /// <param name="fileName"></param>
- /// <returns></returns>
- public static byte[] LoadAssemblyFiles(string fileName)
- {
- if (fileName == null)
- {
- throw new ArgumentNullException("filename");
- }
- string fullFileName = RESOURCE_FILE_PREFIX + fileName;
- byte[] fileContent;
- using (Stream stream = typeof(Utils).Assembly.GetManifestResourceStream(fullFileName))
- {
- fileContent = new byte[stream.Length];
- stream.Read(fileContent, 0, fileContent.Length);
- }
- return fileContent;
- }
- /// <summary>
- /// Get html template from buildin resource file
- /// </summary>
- /// <param name="filename"></param>
- /// <returns></returns>
- public static StringBuilder GetHtml(string filename)
- {
- byte[] buffer = LoadFileFromCache(filename);
- if (buffer == null)
- {
- throw new ArgumentNullException("filename", ("isn`t find " + filename));
- }
- if (buffer.Length == 0)
- {
- throw new ArgumentNullException("filename", ("isn`t find " + filename));
- }
- return new StringBuilder(Encoding.Default.GetString(buffer));
- }
- /// <summary>
- /// Return true if client browser > IE 5.5
- /// </summary>
- /// <returns></returns>
- public static bool IsAccordantBrowser()
- {
- HttpBrowserCapabilities bc = GetContext().Request.Browser;
-
- if(bc.Browser != "IE" || float.Parse(bc.Version) < 5.5 )
- {
- return false;
- }
- return true;
- }
- /// <summary>
- /// Turn file size into a readability format.
- /// </summary>
- /// <param name="size"></param>
- /// <returns></returns>
- public static string GetFormatString(double size)
- {
- string sizeString;
- if (size >= 1048576)
- {
- sizeString = (Math.Round(size/1048576, 2) + " m");
- }
- else if (size >= 1024)
- {
- sizeString = (Math.Round(size/1024, 2) + " k");
- }
- else
- {
- sizeString = (size + " bytes");
- }
- return sizeString;
- }
- /// <summary>
- /// Turn time string into a readability format.
- /// </summary>
- /// <param name="span"></param>
- /// <returns></returns>
- public static string GetFormatString(TimeSpan span)
- {
- string timeString = string.Empty;
- if ((span.Days > 0) || (span.Hours > 0))
- {
- int hours = ((0x18*span.Days) + span.Hours);
- timeString = (timeString + hours + " 小时 ");
- }
- if (span.Minutes > 0)
- {
- timeString = (timeString + span.Minutes + " 分 ");
- }
- if (span.Seconds > 0)
- {
- timeString = (timeString + span.Seconds + " 秒 ");
- }
- return timeString;
- }
- }
- }