Ejemplo n.º 1
0
        /// <summary>
        /// 复制到指定类型资源的路径中
        /// 如果指定文件存在,则覆盖
        /// 成功则返回true
        /// </summary>
        /// <param name="targetResType">目标类型</param>
        /// <returns></returns>
        public ResManage <T> CopyTo(T targetResType)
        {
            if (this.FileExist == false)
            {
                throw new FileNotFoundException("文件不存在", this.FullFileName);
            }

            if (targetResType.Equals(this.ResType) == true)
            {
                throw new ArgumentException("resType参数有误", "resType");
            }

            var match    = Regex.Match(this.FileName, @"$\d{4}_\d{2}\\");
            var fileName = Regex.Replace(this.FileName, @"$\d{4}_\d{2}\\", string.Empty);

            var resTypeEnum = (Enum)Enum.Parse(typeof(T), targetResType.ToString());

            if (resTypeEnum.IsDefined <ResSplitByMonthAttribute>() == true)
            {
                fileName = string.Format(@"{0}\{1}", match.Success ? match.Value : DateTime.Now.ToString("yyyy_MM"), fileName);
            }

            var res = ResManage.Parse(fileName, targetResType);

            Directory.CreateDirectory(Path.GetDirectoryName(res.FullFileName));
            File.Copy(this.FullFileName, res.FullFileName, true);
            return(res);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 保存上传的文件对象到指定类型文件夹下
 /// 返回保存后资源文件的信息
 /// </summary>
 /// <param name="file">上传的文件</param>
 /// <param name="resType">资源类型</param>
 /// <param name="idName">用以作文件名或文件的父文件夹名</param>
 /// <param name="useIdFolder">是否使用idName作父文件夹包裹文件,如果为ture,文件名将不变</param>
 /// <param name="keepName">当useIdFolder为true且keepName为false时,文件名为随机名</param>
 /// <returns></returns>
 public static ResManage <T> SavePostedFile <T>(HttpPostedFileBase file, T resType, Guid idName, bool useIdFolder = false, bool keepName = false) where T : struct
 {
     if (typeof(T).IsEnum == false)
     {
         throw new Exception("泛型参数类型必须为枚举类型");
     }
     return(ResManage.SaveFile(file, resType, idName, useIdFolder, keepName));
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 保存上传的文件对象到指定类型文件夹下
        /// 返回保存后资源文件的信息
        /// </summary>
        /// <param name="file">上传的文件</param>
        /// <param name="resType">资源类型</param>
        /// <param name="idName">用以作文件名或文件的父文件夹名</param>
        /// <param name="useIdFolder">是否使用idName作父文件夹包裹文件,如果为ture,文件名将不变</param>
        /// <param name="keepName">当useIdFolder为true且keepName为false时,文件名为随机名</param>
        /// <returns></returns>
        private static ResManage <T> SaveFile <T>(HttpPostedFileBase file, T resType, object idName, bool useIdFolder = false, bool keepName = false) where T : struct
        {
            if (file == null || file.ContentLength == 0)
            {
                throw new ArgumentNullException("file");
            }

            var    idNameString = idName.ToString().Replace("-", "_");
            string fileName     = string.Concat(idNameString, Path.GetExtension(file.FileName));

            if (useIdFolder == true)
            {
                if (keepName == true)
                {
                    fileName = Path.Combine(idNameString, file.FileName);
                }
                else
                {
                    var randomName = string.Concat(Guid.NewGuid().ToString().Replace("-", "_"), Path.GetExtension(file.FileName));
                    fileName = Path.Combine(idNameString, randomName);
                }
            }

            var resTypeEnum = (Enum)Enum.Parse(typeof(T), resType.ToString());

            if (resTypeEnum.IsDefined <ResSplitByMonthAttribute>() == true)
            {
                fileName = Path.Combine(DateTime.Now.ToString("yyyy_MM"), fileName);
            }

            string fullFileName = Path.Combine(ResManage.RootFullPath, resType.ToString(), fileName);
            string fullPath     = Path.GetDirectoryName(fullFileName);

            Directory.CreateDirectory(fullPath);
            file.SaveAs(fullFileName);

            return(ResManage.Parse(fileName, resType));
        }