Esempio n. 1
0
        public async Task <IActionResult> OnPost()
        {
            if (PdfFile == null || ImgFile == null)
            {
                return(BadRequest());
            }
            else
            {
                Book.CreationDate = DateTime.Now;
                if (ModelState.IsValid)
                {
                    #region pdf file
                    string pdfFileName = Guid.NewGuid().ToString() + Path.GetFileName(PdfFile.FileName);
                    string pdfFilePath = Path.Combine(env.WebRootPath, "files");
                    string fullPath    = Path.Combine(pdfFilePath, pdfFileName);
                    Book.Url = pdfFileName;
                    await PdfFile.CopyToAsync(new FileStream(fullPath, FileMode.Create));

                    #endregion

                    #region img file
                    string imgFileName = Guid.NewGuid().ToString() + Path.GetFileName(ImgFile.FileName);
                    string imgFilePath = Path.Combine(env.WebRootPath, "imgs");
                    string imgFullPath = Path.Combine(imgFilePath, imgFileName);
                    Book.ImgUrl = imgFileName;
                    await ImgFile.CopyToAsync(new FileStream(imgFullPath, FileMode.Create));

                    #endregion

                    await db.Books.AddAsync(Book);

                    await db.SaveChangesAsync();

                    return(RedirectToPage("index"));
                }
                return(Page());
            }
        }