/// <summary>
        /// 单文件文件上传
        /// </summary>
        /// <param name="model"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <IResultModel <FileInfo> > Upload(FileUploadModel model, CancellationToken cancellationToken = default)
        {
            var result = new ResultModel <FileInfo>();

            if (model.FormFile == null || model.FormFile.Length < 1)
            {
                if (model.Request.Form.Files != null && model.Request.Form.Files.Any())
                {
                    model.FormFile = model.Request.Form.Files[0];
                }
            }

            if (model.FormFile == null || model.FormFile.Length < 1)
            {
                return(result.Failed("请选择文件!"));
            }

            var resultModel = await UploadSave(model.FormFile, model.RelativePath, model.RootPath, cancellationToken);

            return(result.Success(resultModel));
        }
Пример #2
0
        /// <summary>
        /// 单文件文件上传
        /// </summary>
        /// <param name="model"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <IResultModel <FileInfo> > Upload(FileUploadModel model, CancellationToken cancellationToken = default)
        {
            var result = new ResultModel <FileInfo>();

            if (model.FormFile == null || model.FormFile.Length < 1)
            {
                if (model.Request.Form.Files != null && model.Request.Form.Files.Any())
                {
                    model.FormFile = model.Request.Form.Files[0];
                }
            }

            if (model.FormFile == null || model.FormFile.Length < 1)
            {
                return(result.Failed("请选择文件!"));
            }

            var name     = model.FileName.IsNull() ? model.FormFile.FileName : model.FileName;
            var size     = model.FormFile.Length;
            var fileInfo = new FileInfo(name, size);

            if (model.MaxSize > 0 && model.MaxSize < size)
            {
                return(result.Failed($"文件大小不能超过{new FileSize(model.MaxSize).ToString()}"));
            }

            if (model.LimitExt != null && !model.LimitExt.Any(m => m.EqualsIgnoreCase(fileInfo.Ext)))
            {
                return(result.Failed($"文件格式无效,请上传{model.LimitExt.Aggregate((x, y) => x + "," + y)}格式的文件"));
            }

            var date = DateTime.Now;

            fileInfo.Path = Path.Combine(model.RelativePath, date.ToString("yyyy"), date.ToString("MM"), date.ToString("dd"));
            var resultModel = await UploadSave(model.FormFile, fileInfo, model.RootPath, model.CalcMd5, cancellationToken);

            return(result.Success(resultModel));
        }