Ejemplo n.º 1
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);
        }