Exemple #1
0
        /// <summary>
        /// 获取文件的只读流
        /// </summary>
        public static Stream GetFileStream(string filePathOrUrl)
        {
            Stream retVal = null;

            try
            {
                if (!string.IsNullOrEmpty(filePathOrUrl))
                {
                    PathType pathType = 0;

                    var targetPath = filePathOrUrl;

                    if (RegExpHelper.IsUrl(filePathOrUrl))
                    {
                        pathType   = PathType.WebUrl;
                        targetPath = filePathOrUrl;
                    }
                    else if (RegExpHelper.IsRelativePath(filePathOrUrl))
                    {
                        pathType   = PathType.WindowPath;
                        targetPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filePathOrUrl.Replace("/", @"\").TrimStart('/').TrimStart('\\'));
                    }
                    else if (RegExpHelper.IsPhysicalPath(filePathOrUrl))
                    {
                        pathType   = PathType.WindowPath;
                        targetPath = filePathOrUrl;
                    }

                    if (Enum.IsDefined(typeof(PathType), pathType))
                    {
                        switch (pathType)
                        {
                        case PathType.WindowPath:
                            retVal = new FileStream(targetPath, FileMode.Open, FileAccess.Read);
                            break;

                        case PathType.WebUrl:
                            retVal = NetHelper.HttpGetStream(targetPath);
                            break;
                        }
                    }
                }
            }
            catch { }
            return(retVal);
        }