Ejemplo n.º 1
0
 public ActionResult AddFiles(int id)
 {
     var page = staticPagesRepository.GetById(id);
     var viewModel = new StaticPageViewModel()
         {
             Id = page.Id,
             Title = page.Title,
             Section = (StaticPageSection)page.Section
         };
     return View(viewModel);
 }
Ejemplo n.º 2
0
        public ActionResult AddFiles(StaticPageViewModel viewModel, IEnumerable<HttpPostedFileBase> files)
        {
            if (ModelState.IsValid)
            {
                var fileKeys = Request.Files.AllKeys;
                var page = staticPagesRepository.GetById(viewModel.Id);

                foreach (var key in fileKeys)
                {
                    if (Request.Files[key] == null || Request.Files[key].ContentLength == 0)
                        continue;
                    using (new UnitOfWork(_currentContext))
                    {
                        var file = Request.Files[key];
                        //upload file first
                        var fileName = file.FileName;
                        var absolutePath = Server.MapPath("~/Content/uploads/staticpages/" + viewModel.Id.ToString(CultureInfo.InvariantCulture));
                        var fullVirtualPath = "~/Content/uploads/staticpages/" + viewModel.Id.ToString(CultureInfo.InvariantCulture) + "/" + fileName;
                        var directory = Server.MapPath("~/Content/uploads/staticpages/" + viewModel.Id.ToString(CultureInfo.InvariantCulture));

                        if (!Directory.Exists(directory))
                            Directory.CreateDirectory(directory);

                        //create a temp file first, then compress it
                        FileUploader.UploadFile(file, fileName,
                                                absolutePath);
                        var uploadedFile = new File()
                            {
                                FileName = fullVirtualPath,
                                FilePurposeId = (int) FilePurposes.StaticPage
                            };

                        page.Files.Add(uploadedFile);
                    }
                }

                TempData[Const.ActionResultInfo] = "Файлы успешно прикреплены к странице";
                return RedirectToAction("StaticPageFiles", new {id = page.Id});
            }

            TempData[Const.ActionErrorInfo] = "Возникла ошибка загрузки файлов. Попробуйте позже";
            return View(viewModel);
        }