Exemple #1
0
        /// <summary>
        /// 根据前两个字节获取文件后缀名,获取不到时使用文件名截取方法获取
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="filecontent"></param>
        /// <returns></returns>
        public static string GetFileExt(string filename, byte[] filecontent)
        {
            var result = string.Empty;

            if (filecontent != null && filecontent.Length > 1)
            {
                var fileCode = filecontent[0].ToString() + filecontent[1].ToString();
                switch ((FileExtension)IntegerCore.Parse(fileCode))
                {
                case FileExtension.JPG:
                    result = "jpg";
                    break;

                case FileExtension.JS:
                    result = "js";
                    break;

                case FileExtension.GIF:
                    result = "gif";
                    break;

                case FileExtension.BMP:
                    result = "bmp";
                    break;

                case FileExtension.PNG:
                    result = "png";
                    break;

                case FileExtension.RAR:
                    result = "rar";
                    break;

                //case FileExtension.ZIP:
                //    result = "zip";
                //    break;
                case FileExtension.MP3:
                    result = "mp3";
                    break;

                default:
                    if (filecontent[0] == 0 && filecontent[1] == 0 && filecontent[2] == 0)
                    {
                        result = "mp4";
                    }
                    break;
                }
            }
            else if (!string.IsNullOrEmpty(filename) && filename.IndexOf('.') > -1)
            {
                result = filename.Substring(filename.LastIndexOf('.') + 1);
            }
            return(result);
        }
Exemple #2
0
        /// <summary>
        /// 判断文件是否为安全文件
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="exts"></param>
        /// <returns></returns>
        public static bool IsSafeFile(byte[] content, params FileExtension[] exts)
        {
            if (content == null || content.Length < 2 || exts == null || exts.Length < 1)
            {
                return(false);
            }

            var typeStr  = content[0].ToString() + content[1].ToString();
            var fileType = (FileExtension)IntegerCore.Parse(typeStr);

            return(exts.Contains(fileType));
        }