Пример #1
0
        public async Task <ActionResult> Upload(UploadCourseViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }

            if (!viewModel.CheckFileExtentions(viewModel.File.FileName))
            {
                return(View(viewModel));
            }

            var reader = new BinaryReader(viewModel.File.InputStream);
            var data   = reader.ReadBytes(viewModel.File.ContentLength);
            var path   = Server.MapPath("~/uploads/");

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            viewModel.File.SaveAs(Path.Combine(path, viewModel.File.FileName));
            var urlBase    = ConfigurationManager.AppSettings["SiteUrlBase"];
            var fileUpload = new SubmissionFile
            {
                FileName      = viewModel.File.FileName,
                ContentType   = viewModel.File.ContentType,
                ContentLength = viewModel.File.ContentLength,
                ExternalUrl   = $"{urlBase}uploads/{viewModel.File.FileName}",
                Data          = data,
                SubmissionId  = viewModel.SubmissionId,
                CreateDate    = DateTime.UtcNow
            };

            var currentSubmission = await _context.Submissions.FirstOrDefaultAsync(i => i.Id == fileUpload.SubmissionId);

            currentSubmission.SubmissionFiles = new List <SubmissionFile>();
            currentSubmission.SubmissionFiles.Add(fileUpload);

            await _uploadService.AddAsync(fileUpload);

            return(RedirectToRoute("SubmissionDetails", new { submissionId = currentSubmission.Id }));
        }