public async Task <ActionResult> AddFile([FromBody] AddFileDto fileDto) { if (!IsAvailableOperation()) { return(BadRequest()); } await _fileStorageService.AddFile(fileDto, UserId, ClientId); AddLog(Enums.LogType.Create, LogMessage.CreateSuccessByNameMessage(LogMessage.FileEntityName, fileDto.Name, LogMessage.CreateAction, UserId)); return(Ok()); }
public async Task <CommonResponse <AddFileResult> > AddFile(IFormFile file) { var response = new CommonResponse <AddFileResult>(); try { var id = Guid.NewGuid().ToString("N"); var suffix = file.FileName.Substring(file.FileName.LastIndexOf(".") + 1); var filePath = $"{_localPath}{id}.{suffix}";//注意formFile.FileName包含上传文件的文件路径,所以要进行Substring只取出最后的文件名 using (var stream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(stream); } var entity = new dal.Entity.FileStorage { Id = id, ContentType = file.ContentType, CreateTime = DateTime.Now, StoragePath = filePath, StorageType = dal.Entity.FileStorageType.Local, Size = file.Length, AccessUrl = $"{_gatewayFilePath}?f={id}", BizType = dal.Entity.FileStorageBizType.SignImage, OriginalFileName = file.FileName }; await _fileStorageService.AddFile(entity); response.Data = new AddFileResult { Id = id, AccessUrl = entity.AccessUrl, Size = file.Length, ContentType = file.ContentType, FileName = file.FileName }; response.Success = true; } catch { response.Message = "上传发生异常"; } return(response); }