public async Task <IActionResult> EditBookAsync([FromForm] BookViewModel model) { var entity = model.ToEntity(); if (model.BookId == 0) { await _booksServices.CreatedBookAsync(entity); if (model.UploadedPicture != null) { await _pictureService.AddPicture(entity.Id, model.UploadedPicture, true); } } else { entity.Id = model.BookId; if (model.UploadedPicture != null) { if (model.ImageIsMain) { await _pictureService.DeleteMainPicture(entity.Id); } await _pictureService.AddPicture(entity.Id, model.UploadedPicture, model.ImageIsMain); } await _booksServices.UpdateBook(entity); } return(Redirect("/")); }
public HttpResponseMessage AddPicture(PictureDto picture) { try { _pictureService.AddPicture(picture); return(Request.CreateResponse(HttpStatusCode.Created, "Successfully added a picture!")); } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message)); } }
//上传图片 public async Task <IActionResult> Picture([FromServices] IWebHostEnvironment env, [FromForm] IFormFile imgFile) { if (imgFile != null && imgFile.Length > 0 && imgFile.Length < 20971520) { var fileExt = Path.GetExtension(imgFile.FileName); var filePath = Path.Combine(env.WebRootPath, Guid.NewGuid().ToString()); var picture = new EditPictureViewModel { Origin = $"{filePath}_Origin{fileExt}", Big = $"{filePath}_Big{fileExt}", Small = $"{filePath}_Small{fileExt}" }; using (var stream = imgFile.OpenReadStream()) { FormatImage.AutoToSmall(stream, picture.Big, 1080); FormatImage.AutoToSmall(stream, picture.Small, 260); } await _pictureService.AddPicture(picture); } return(View()); }
//上传图片 public async Task <IActionResult> Picture([FromServices] IWebHostEnvironment env, [FromForm] IFormFile imgFile) { //图片最大<18M if (imgFile != null && imgFile.Length > 0 && imgFile.Length < 18874368) { //图片扩展名 var fileExt = Path.GetExtension(imgFile.FileName); var rootPath = env.WebRootPath; //按年月归档图片 var now = DateTime.Now; var timePath = Path.Combine(now.Year.ToString(), now.Month.ToString()); var filePath = Path.Combine("photo", timePath); //创建图片目录 if (!Directory.Exists(Path.Combine(rootPath, filePath))) { Directory.CreateDirectory(Path.Combine(rootPath, filePath)); } filePath = Path.Combine(filePath, Guid.NewGuid().ToString()); var picture = new EditPictureViewModel { //原图可能太大,暂时不保存 Origin = $"{filePath}_origin{fileExt}", Big = $"{filePath}_big{fileExt}", Small = $"{filePath}_small{fileExt}" }; using (var stream = imgFile.OpenReadStream()) { //为了不影响图片观感,建议图片质量值设置为80以上 await FormatImage.AutoToSmall(stream, Path.Combine(rootPath, picture.Big), 1920, 99); await FormatImage.AutoToSmall(stream, Path.Combine(rootPath, picture.Small), 1080, 99); } await _pictureService.AddPicture(picture); } return(View()); }
public async Task <IActionResult> AddPictures(List <IFormFile> files, int id) { var authorId = this.userManager.GetUserId(User); var product = await productService.GetProductByIdAsync(id); if (!IsUserAuthorizedToEdit(product.OwnerId, authorId)) { return(Forbid()); } // Check whether pictures count >= picturesMaxCount int picsCount = productService.GetProductPicturesCount(id); if (picsCount >= PicturesPerProductMaxCount) { ViewBag.Error = string.Format("You cannot upload more than {0} images to this product!", PicturesPerProductMaxCount); return(RedirectToAction(nameof(ProductController.AddPictures), "Product")); } var uploadDirectory = Path.Combine(hostingEnvironment.WebRootPath, "images\\Products"); foreach (var file in files) { // Validate file format string extension = Path.GetExtension(file.FileName); if (!IsExtensionValid(extension)) { ViewBag.Error = "Invalid file format!"; return(RedirectToAction(string.Concat(nameof(ProductController.AddPictures), "/", product.Id), "Product")); } var fullPath = Path.Combine(uploadDirectory, GetUniqueFileName(file.FileName)); var uniqueFileName = fullPath .Substring(fullPath.IndexOf(Path.GetFileNameWithoutExtension(file.FileName))); var dbPath = $"/images/Products/{uniqueFileName}"; // Add the original picture to filesystem using (var stream = new FileStream(fullPath, FileMode.Create)) { file.CopyTo(stream); } // Resize the picture: Image imgOriginal = Image.FromFile(fullPath); Image resizedImg = ResizePicture(imgOriginal, PictureMaxWidth, PictureMaxHeight); imgOriginal.Dispose(); // Add the resized picture to filesystem resizedImg.Save(fullPath); // Add the current picture to database pictureService.AddPicture(dbPath, id, authorId); } return(RedirectToAction(string.Concat(nameof(ProductController.Details), "/", product.Id), "Product")); }
public async Task <Guid> Create(PictureModel pictureModel) { return(await _pictureService.AddPicture(pictureModel)); }