public async Task <IActionResult> OnPostAsync(ICollection <IFormFile> file, string groups) { string yyyymm = DateTime.Now.ToyyyyMM(); var config = await _configService.GetAsync(); string oppositePath = $"pictures/{User.GetUserIdOrZero()}/{yyyymm}/"; var folder = _env.WebRootPath + "/upload/" + oppositePath; if (config != null && !string.IsNullOrEmpty(config.PictureSavePath)) { folder = Path.Combine(config.PictureSavePath, oppositePath); } if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } long[] groupIds = groups.SplitToInt64(); //检查素材分组与用户关系 foreach (var id in groupIds) { var group = await _groupService.GetByIdAsync(id); if (group == null || group.Type != Domain.Enum.MaterialCenter.EmUserMaterialGroupType.Picture || group.UserId != User.GetUserIdOrZero()) { throw new KuException("选择的素材分组不正确!"); } } foreach (var item in file) { PictureDto model = new PictureDto(); model.Id = ID.NewID(); string suffix = item.FileName.Split('.').Last().ToLower(); var saveName = model.Id + "_original." + suffix; using (var fileStream = new FileStream(Path.Combine(folder, saveName), FileMode.Create)) { await item.CopyToAsync(fileStream); await fileStream.FlushAsync(); } model.Title = item.FileName; model.FileName = saveName; model.Folder = oppositePath; model.FilePath = oppositePath + saveName; model.FileSize = item.Length; model.FileType = suffix; model.UploadUserId = User.GetUserIdOrZero(); await _service.AddAsync(model, groupIds); } return(Json(true)); }
public async Task <ActionResult> AddPicture([FromForm] PictureCreateModel model) { var userId = User.Claims.ElementAt(0).Value; using (var stream = new MemoryStream()) { await model.Image.CopyToAsync(stream); await _service.AddAsync(new PictureDTO { Name = model.Name, Image = new Bitmap(stream), UserId = userId } ); } return(Ok()); }