⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 common.cs

📁 PowUpload的C#源文件,用来做大文件上传的项目
💻 CS
字号:
namespace ElementIT.PowUpload
{
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Reflection;
    using System.Web;

    internal class Common
    {
        internal static object GetConfigValue(string section, string key, bool generateexception, object defaultValue)
        {
            IDictionary config = (IDictionary) ConfigurationSettings.GetConfig("Element-IT.PowUpload/" + section);
            if (config == null)
            {
                if (generateexception)
                {
                    throw new Exception(string.Format("Section {0} not found in the Web.config file. Specify it at the \"Element-IT.PowUpload\" section.", section));
                }
                return defaultValue;
            }
            if (config[key] == null)
            {
                if (generateexception)
                {
                    throw new Exception(string.Format("Key {0} not found in the Web.config file. Specify it at the {1} section of the \"Element-IT.PowUpload\" section.", key, section));
                }
                return defaultValue;
            }
            if (string.Compare((string) config[key], "", false) == 0)
            {
                if (generateexception)
                {
                    throw new Exception(string.Format("Required key {0} is empty in the Web.config file. Specify it at the {1} section of the \"Element-IT.PowUpload\" section.", key, section));
                }
                return defaultValue;
            }
            return config[key];
        }

        internal static object GetWebConfigValue(string section, string key, bool generateexception, object defaultValue)
        {
            object config = null;
            Type type = null;
            object obj3;
            BindingFlags bindingAttr = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
            try
            {
                config = HttpContext.Current.GetConfig(section);
                type = config.GetType();
                try
                {
                    obj3 = type.GetProperty(key, bindingAttr).GetValue(config, null);
                }
                catch
                {
                    key = key.Substring(0, 1).ToUpper() + key.Substring(1, key.Length - 1);
                    obj3 = type.GetProperty(key, bindingAttr).GetValue(config, null);
                }
            }
            catch
            {
                if (generateexception)
                {
                    throw new Exception(string.Format("Required key {0} is empty in the Web.config file. Specify it at the {1} section of the \"Element-IT.PowUpload\" section.", key, section));
                }
                obj3 = defaultValue;
            }
            return obj3;
        }

        internal static void SetFormBodyAtWorkerRequest(HttpWorkerRequest request, byte[] contentBody, HttpApplication app)
        {
            BindingFlags bindingAttr = BindingFlags.NonPublic | BindingFlags.Instance;
            int length = contentBody.Length;
            Type baseType = request.GetType();
            while (baseType != null)
            {
                if ((string.Compare(baseType.FullName, "System.Web.Hosting.ISAPIWorkerRequest", true) == 0) || (string.Compare(baseType.FullName, "Microsoft.VisualStudio.WebHost.Request", true) == 0))
                {
                    break;
                }
                baseType = baseType.BaseType;
            }
            if ((baseType != null) && (string.Compare(baseType.FullName, "System.Web.Hosting.ISAPIWorkerRequest", true) == 0))
            {
                baseType.GetField("_contentAvailLength", bindingAttr).SetValue(request, length);
                baseType.GetField("_contentTotalLength", bindingAttr).SetValue(request, length);
                baseType.GetField("_preloadedContent", bindingAttr).SetValue(request, new byte[length]);
                Array.Copy(contentBody, 0, (byte[]) baseType.GetField("_preloadedContent", bindingAttr).GetValue(request), 0, length);
                baseType.GetField("_preloadedContentRead", bindingAttr).SetValue(request, true);
            }
            if ((baseType != null) && (string.Compare(baseType.FullName, "Microsoft.VisualStudio.WebHost.Request", true) == 0))
            {
                baseType.GetField("_contentLength", bindingAttr).SetValue(request, length);
                baseType.GetField("_preloadedContent", bindingAttr).SetValue(request, new byte[length]);
                Array.Copy(contentBody, 0, (byte[]) baseType.GetField("_preloadedContent", bindingAttr).GetValue(request), 0, length);
                baseType.GetField("_preloadedContentLength", bindingAttr).SetValue(request, length);
            }
            if (baseType == null)
            {
                baseType = request.GetType();
                if (baseType != null)
                {
                    try
                    {
                        baseType.GetField("_contentAvailLength", bindingAttr).SetValue(request, length);
                    }
                    catch
                    {
                    }
                    try
                    {
                        baseType.GetField("_contentLength", bindingAttr).SetValue(request, length);
                    }
                    catch
                    {
                    }
                    try
                    {
                        baseType.GetField("_contentTotalLength", bindingAttr).SetValue(request, length);
                    }
                    catch
                    {
                    }
                    try
                    {
                        baseType.GetField("_preloadedContentLength", bindingAttr).SetValue(request, length);
                    }
                    catch
                    {
                    }
                    try
                    {
                        baseType.GetField("_preloadedContent", bindingAttr).SetValue(request, new byte[length]);
                        Array.Copy(contentBody, 0, (byte[]) baseType.GetField("_preloadedContent", bindingAttr).GetValue(request), 0, length);
                    }
                    catch
                    {
                    }
                    try
                    {
                        baseType.GetField("_preloadedContentRead", bindingAttr).SetValue(request, true);
                    }
                    catch
                    {
                    }
                }
            }
            Type type2 = null;
            BindingFlags flags2 = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
            type2 = app.Request.GetType();
            try
            {
                type2.GetField("_contentLength", flags2).SetValue(app.Request, length);
            }
            catch
            {
            }
        }

        internal static bool StrToBln(string str)
        {
            return ((string.Compare(str.ToUpper(), "TRUE") == 0) || ((string.Compare(str.ToUpper(), "YES") == 0) || (string.Compare(str.ToUpper(), "1") == 0)));
        }

        internal static string toRFC1123DateFormat(DateTime date)
        {
            return date.ToString("r");
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -