コード例 #1
0
        public async Task <IActionResult> OnPost()
        {
            if (ModelState.IsValid)
            {
                #region files code
                if (PdfFile != null || ImgFile != null)
                {
                    if (PdfFile != null)
                    {
                        #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
                    }
                    if (ImgFile != null)
                    {
                        #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
                    }
                }
                #endregion
                var oldBook = await db.Books.FirstOrDefaultAsync(b => b.Id == id);

                if (oldBook == null)
                {
                    return(NotFound());
                }
                oldBook.Name           = Book.Name;
                oldBook.Auther         = Book.Auther;
                oldBook.CategoryId     = Book.CategoryId;
                oldBook.LangCategoryId = Book.LangCategoryId;
                oldBook.Description    = Book.Description;

                db.Entry(oldBook).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToPage("index"));
            }
            return(Page());
        }
コード例 #2
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());
            }
        }