示例#1
0
        /// <summary>
        /// 根据当前日期,指定跟目录和文件扩展名获取一个完整的存储路径和名称
        /// </summary>
        /// <param name="path"></param>
        /// <param name="name">文件名称,为null时使用随机文件名</param>
        /// <param name="extension">文件扩展名</param>
        /// <returns></returns>
        public static string AppendFileNameByDate(this string path, string name, string extension)
        {
            if (string.IsNullOrEmpty(extension))
            {
                throw new ArgumentNullException(nameof(extension));
            }

            if (extension.StartsWith("."))
            {
                extension = extension.TrimStart('.');
            }

            path = path.AppendFileDirectoryByDate();

            if (string.IsNullOrWhiteSpace(name))
            {
                name = RandomCode.GetDtString(6);
            }

            StringBuilder fileName = new StringBuilder(path);

            fileName.AppendFormat("/{0}/{1}.{2}", path, name, extension);

            return(fileName.ToString());
        }
示例#2
0
        /// <summary>
        /// 由上传路径、文件名及扩展名,构造一个完整的存储路径和名称
        /// </summary>
        /// <param name="path">上传路径</param>
        /// <param name="name">文件名</param>
        /// <param name="extension">扩展名</param>
        /// <returns></returns>
        public static string GetFileName(this string path, string name, string extension)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                path = path.AppendFileDirectoryByDate();
            }

            if (path.StartsWith("/"))
            {
                path = path.TrimStart('/');                      //移除头部"/"字符
            }
            if (path.EndsWith("/"))
            {
                path = path.TrimEnd('/');                    //移除尾部"/"字符
            }
            if (string.IsNullOrWhiteSpace(name))
            {
                name = RandomCode.GetTimeString(6);
            }

            if (extension.StartsWith("."))
            {
                extension = extension.TrimStart('.');
            }

            return(string.Format("/{0}/{1}.{2}", path, name, extension));
        }
示例#3
0
        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="file"><seealso cref="IFormFile"/>文件</param>
        /// <param name="savepath">上传目录</param>
        /// <param name="fixedpath">是否固定存储在指定的savepath目录下,为false时在savepath目录后添加日期目录</param>
        /// <param name="name">保存文件名(不含扩展名)</param>
        /// <param name="beOverride">文件存在时是否覆盖</param>
        /// <returns></returns>
        public static UploadResult Save(this IFormFile file, string savepath, bool fixedpath, string name, bool beOverride = false)
        {
            var result = new UploadResult();

            if (null == file)
            {
                result.Message = "没有可上传的内容";
                return(result);
            }

            if (file.Length > Configs.UploadOptions.LimitSize * 1024)
            {
                result.Message = string.Format("上传的文件不能超过{0}KB", Configs.UploadOptions.LimitSize);
                return(result);
            }

            //标识字段名
            result.FieldName = file.GetFieldName();

            //文件名
            string fileName = file.FileName;

            if (string.IsNullOrWhiteSpace(name))
            {
                name = RandomCode.GetDtString(3);
            }
            else
            {
                name = name.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries)[0];
            }

            //扩展名
            string extension = Path.GetExtension(file.FileName);

            //固定在指定的上传目录
            if (fixedpath)
            {
                if (string.IsNullOrWhiteSpace(savepath))
                {
                    savepath = Configs.UploadOptions.SaveRoot;
                }
            }
            //非固定指定上传目录时,在上传目录中加入时期目录层
            else
            {
                savepath = savepath.AppendFileDirectoryByDate();
            }

            //文件保存后文件全名(如:/upload/2016/12/10/231.jpg)
            var absFileName = savepath.GetFileName(name, extension);

            //新上传后的存储路径(如:D:\\upload\\2016\\12\\10\\45341.jpg)
            var rawFilePath = Path.Combine(Configs.UploadOptions.PhysicalUploadRoot, absFileName.TrimStart('/').Replace("/", "\\"));

            if (File.Exists(rawFilePath))
            {
                if (beOverride)
                {
                    File.Delete(rawFilePath);
                }
                else
                {
                    GetLastVersionFile(absFileName, 1, false, out absFileName, out rawFilePath);
                }
            }

            //物理目录
            var rawDirectory = Path.GetDirectoryName(rawFilePath);

            try
            {
                if (!Directory.Exists(rawDirectory))
                {
                    Directory.CreateDirectory(rawDirectory);
                }
            }
            catch
            {
                result.Message = "创建目录失败";
                return(result);
            }

            try
            {
                using (FileStream fs = File.Create(rawFilePath))
                {
                    file.CopyToAsync(fs).Wait();
                }

                result.FilePath = absFileName;
            }
            catch
            {
                result.Message = "文件存储失败";
            }

            return(result);
        }