public async Task <bool> UpLoadFile(List <IFormFile> files, BaiPost model) { if (files == null) { return(true); } string[] permittedExtensions = { ".png", ".jpeg", ".jpg" }; string uniqueFileName = null; foreach (var file in files) { ImgBaiPost img = new ImgBaiPost(); var ext = Path.GetExtension(file.FileName).ToLowerInvariant(); if (string.IsNullOrEmpty(ext) || !permittedExtensions.Contains(ext)) { return(false); } string UploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath, "img/ThaoLuan"); uniqueFileName = Guid.NewGuid().ToString() + "_" + file.FileName; string filePath = Path.Combine(UploadsFolder, uniqueFileName); await file.CopyToAsync(new FileStream(filePath, FileMode.Create)); ImgBaiPost imgBaiPost = new ImgBaiPost { IdbaiPost = model.Id, TenAnh = file.FileName, KichThuoc = FileSizeFormatter.FormatSize(file.Length), AnhDinhKem = uniqueFileName }; model.ImgBaiPost.Add(imgBaiPost); } return(true); }
public bool DeleteImg(int[] idImg, BaiPost model) { foreach (int i in idImg) { ImgBaiPost imgBaiPost = model.ImgBaiPost.SingleOrDefault(x => x.Id == i); if (imgBaiPost == null) { return(false); } model.ImgBaiPost.Remove(imgBaiPost); } return(true); }