示例#1
0
        public async Task <IActionResult> AddNewBook(BookModel book)
        {
            if (book.CoverImage != null)
            {
                string folder           = "imgs/userUploadImages";
                string serverRootPath   = _webHostEnvironment.WebRootPath;
                string fileName         = Guid.NewGuid().ToString() + "_" + book.CoverImage.FileName;
                string serverFolderPath = Path.Combine(serverRootPath, folder, fileName);

                await book.CoverImage.CopyToAsync(new FileStream(serverFolderPath, FileMode.Create));

                //这是供前端使用的URL地址,在HTML中需要在图片地址前加上"/"
                book.CoverImageUrl = "/" + Path.Combine(folder, fileName);
            }
            if (book.BookPdf != null)
            {
                string folder           = "pdfs";
                string serverRootPath   = _webHostEnvironment.WebRootPath;
                string fileName         = Guid.NewGuid().ToString() + "_" + book.BookPdf.FileName;
                string serverFolderPath = Path.Combine(serverRootPath, folder, fileName);

                await book.BookPdf.CopyToAsync(new FileStream(serverFolderPath, FileMode.Create));

                book.BookPdfUrl = "/" + Path.Combine(folder, fileName);
            }
            int id = _bookRepository.CreateABook(book);

            if (id > 0)
            {
                return(RedirectToAction(nameof(AddNewBook), new { isSuccess = true, bookId = id }));
            }
            return(View());

            //直接重定向到被创建的书记页面
            //return RedirectToAction("BookDetail", new { id });
        }