public IActionResult Create(UploadCreateViewModel model)
        {
            if (ModelState.IsValid)

            {
                string uniqueFileName = null;

                if (model.Project != null)
                {
                    string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "projects");
                    uniqueFileName = model.Project.FileName;
                    string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                    using (var fileStream = new FileStream(filePath, FileMode.Create))
                    {
                        model.Project.CopyTo(fileStream);
                    }
                }
                Upload newUpload = new Upload
                {
                    Name          = model.Name,
                    Description   = model.Description,
                    FileExtention = Path.GetExtension(model.Name),
                    Size          = (model.Length / 1024),// For get size in KB
                    Icon          = GetIconPath(Path.GetExtension(model.Name)),
                    UploadPath    = uniqueFileName
                };

                _uploadRepository.Add(newUpload);
                return(RedirectToAction("details", new { id = newUpload.Id }));
            }

            return(View());
        }
Example #2
0
        public Upload Upload(HttpPostedFileBase file, UploadLocationEnum location, Citizen citizen)
        {
            var filePath = GetUniqueFilePath(location, ".png");

            Directory.CreateDirectory(Path.GetDirectoryName(filePath));
            file.SaveAs(filePath);

            var fileName = Path.GetFileName(filePath);
            var upload   = new Upload()
            {
                UploadedByCitizenID = citizen.ID,
                Day              = GameHelper.CurrentDay,
                Time             = DateTime.Now,
                UploadLocationID = (int)location,
                Filename         = fileName,
            };

            uploadRepository.Add(upload);
            uploadRepository.SaveChanges();

            return(upload);
        }