예제 #1
0
        public async Task <UploadFileResult> UploadFileAsync(IFormFile file, string path)
        {
            string FileExtension = Path.GetExtension(file.FileName);
            var    types         = FileExtentions.FileType.PDF;
            bool   result        = true;

            using (var memory = new MemoryStream())
            {
                await file.CopyToAsync(memory);

                result = FileExtentions.IsValidFile(memory.ToArray(), types, FileExtension.Replace('.', ' '));
                if (result)
                {
                    using (var stream = new FileStream(path, FileMode.Create))
                    {
                        await file.CopyToAsync(stream);
                    }

                    return(new UploadFileResult(true, null));
                }
                else
                {
                    return(new UploadFileResult(false, new List <string>()
                    {
                        "فایل انتخاب شده معتبر نمی باشد."
                    }));
                }
            }
        }
예제 #2
0
        public async Task <IActionResult> InsertOrUpdateBookImage(ImageBookViewModel ViewModel)
        {
            if (ModelState.IsValid)
            {
                var Book = await _unitofwork.BaseRepository <Book>().FindByIDAsync(ViewModel.BookID);

                using (var memorySteam = new MemoryStream())
                {
                    string FileExtension = Path.GetExtension(ViewModel.Image.FileName);
                    await ViewModel.Image.CopyToAsync(memorySteam);

                    var  types  = FileExtentions.FileType.Image;
                    bool result = FileExtentions.IsValidFile(memorySteam.ToArray(), types, FileExtension.Replace('.', ' '));
                    if (result)
                    {
                        Book.Image = memorySteam.ToArray();
                        await _unitofwork.Commit();

                        ViewModel.ImageByte       = memorySteam.ToArray();
                        TempData["Notifications"] = "آپلود فایل با موفقیت انجام شد.";
                    }
                    else
                    {
                        ModelState.AddModelError("", "فایل تصویر کتاب نامعتبر است.");
                    }
                }
            }
            return(PartialView("_InsertOrUpdateBookImage", ViewModel));
        }
예제 #3
0
        public async Task <bool> CreateBookAsync(BooksCreateEditViewModel ViewModel)
        {
            try
            {
                byte[] Image = null;
                if (!string.IsNullOrWhiteSpace(ViewModel.ImageBase64))
                {
                    Image = Convert.FromBase64String(ViewModel.ImageBase64);
                }
                List <Book_Translator> translators = new List <Book_Translator>();
                List <Book_Category>   categories  = new List <Book_Category>();
                if (ViewModel.TranslatorID != null)
                {
                    translators = ViewModel.TranslatorID.Select(a => new Book_Translator {
                        TranslatorID = a
                    }).ToList();
                }
                if (ViewModel.CategoryID != null)
                {
                    categories = ViewModel.CategoryID.Select(a => new Book_Category {
                        CategoryID = a
                    }).ToList();
                }

                DateTime?PublishDate = null;
                if (ViewModel.IsPublish == true)
                {
                    PublishDate = DateTime.Now;
                }
                Book book = new Book()
                {
                    ISBN         = ViewModel.ISBN,
                    IsPublish    = ViewModel.IsPublish,
                    NumOfPages   = ViewModel.NumOfPages,
                    Stock        = ViewModel.Stock,
                    Price        = ViewModel.Price,
                    LanguageID   = ViewModel.LanguageID,
                    Summary      = ViewModel.Summary,
                    Title        = ViewModel.Title,
                    Image        = Image,
                    PublishYear  = ViewModel.PublishYear,
                    PublishDate  = PublishDate,
                    Weight       = ViewModel.Weight,
                    PublisherID  = ViewModel.PublisherID,
                    Author_Books = ViewModel.AuthorID.Select(a => new Author_Book {
                        AuthorID = a
                    }).ToList(),
                    book_Tranlators = translators,
                    book_Categories = categories,
                    File            = ViewModel.FileName,
                };

                if (ViewModel.Image != null)
                {
                    using (var memorySteam = new MemoryStream())
                    {
                        string FileExtension = Path.GetExtension(ViewModel.Image.FileName);
                        await ViewModel.Image.CopyToAsync(memorySteam);

                        var  types  = FileExtentions.FileType.Image;
                        bool result = FileExtentions.IsValidFile(memorySteam.ToArray(), types, FileExtension.Replace('.', ' '));
                        if (result)
                        {
                            book.Image = memorySteam.ToArray();
                        }
                    }
                }

                await _UW.BaseRepository <Book>().CreateAsync(book);

                await _UW.Commit();

                return(true);
            }

            catch
            {
                return(false);
            }
        }