public async Task <IActionResult> OnPostUploadFile()
        {
            ClearMessages();
            try
            {
                if (FileUpload == null)
                {
                    return(RedirectToAction("Index", new { ErrorMessage = "Please select a file to upload." }));
                }

                string filePath = _environment.ContentRootPath + "\\wwwroot\\Uploads\\" + FileUpload.FileName;
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    await FileUpload.CopyToAsync(fileStream);
                }
                SuccessMessage = "File uploaded successfully.";
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

                return(RedirectToAction("Index", new { ErrorMessage = "Unable to upload file." }));
            }

            return(RedirectToAction("Index", new { SuccessMessage = "File Uploaded Successfully." }));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!_tokenService.Tokens.Contains(BlazorToken) ||
                FileUpload.Length > 10485760 ||
                FileUpload.ContentType != MediaTypeNames.Image.Jpeg)
            {
                return(new UnauthorizedResult());
            }

            // Make the directory
            string newDirectoryPath = Path.Combine(_toolLocations.Value.TmpStorage, BlazorToken);

            if (IsDirectoryTraversal(newDirectoryPath))
            {
                return(new UnauthorizedResult());
            }
            Directory.CreateDirectory(newDirectoryPath);

            // Save the file
            string newFilePath = Path.Combine(newDirectoryPath, BlazorToken + ".jpg");

            await using FileStream stream = System.IO.File.Create(newFilePath);
            await FileUpload.CopyToAsync(stream);

            _tokenService.Tokens.Remove(BlazorToken);
            return(new EmptyResult());
        }
 public async Task OnPostAsync()
 {
     if (FileUploads != null)
     {
         foreach (var FileUpload in FileUploads)
         {
             var file = Path.Combine(_environment.ContentRootPath, "uploads", FileUpload.FileName);
             using (var fileStream = new FileStream(file, FileMode.Create)) {
                 await FileUpload.CopyToAsync(fileStream);
             }
         }
     }
 }
Exemplo n.º 4
0
        public async Task OnPostAsync()
        {
            if (!User.Identity.IsAuthenticated)
            {
                return;
            }
            string savedFilePath = Path.Combine(MiscUtils.MapPath("wwwroot/"),
                                                Regex.Replace(FileDirectory, "^\\/*", string.Empty), FileUpload.FileName.Replace("/", string.Empty));

            if (MiscUtils.IsDirectoryTraversal(savedFilePath))
            {
                return;
            }
            await using var stream = System.IO.File.Create(savedFilePath);
            await FileUpload.CopyToAsync(stream);
        }
        public async Task <IActionResult> OnPostAsync(int id)
        {
            var queryEmail = from aDL in _context.Deadline
                             join aEV in _context.Article on aDL.ArticleId equals aEV.ID
                             join aC in _context.Courses on aEV.courseId equals aC.course_Id
                             join aRC in _context.RegisterCourse on aC.course_Id equals aRC.CourseId
                             join aU in _context.Users on aRC.UserId equals aU.Id
                             select new { aDL, aEV, aC, aRC, aU };


            foreach (var item in queryEmail)
            {
                Email = item.aU.Email;
            }

            if (agreeSubmit)
            {
                if (FileUploads != null)
                {
                    string supportedTypes = ".pdf";

                    foreach (var FileUpload in FileUploads)
                    {
                        string[] arrListStr = FileUpload.FileName.Split('.');
                        if (supportedTypes.Contains(arrListStr[1]))
                        {
                            string mess = "You was submited" + FileUpload.FileName;

                            await _emailSender.SendEmailAsync("*****@*****.**", mess, "Submited");

                            string path = "wwwroot\\uploads";

                            var file = Path.Combine(_environment.ContentRootPath, path, FileUpload.FileName);
                            using (var fileStream = new FileStream(file, FileMode.Create))
                            {
                                await FileUpload.CopyToAsync(fileStream);
                            }

                            UserFile = new UserFile()
                            {
                                Title           = FileUpload.FileName,
                                file_IsSelected = false,
                                file_DeadlineId = id,
                                file_CreateBy   = "Thai Bao"
                            };
                            _context.userFiles.Add(UserFile);
                            await _context.SaveChangesAsync();

                            mess = "";
                        }
                        else
                        {
                            mess = "Only .pdf";
                        }
                    }
                }
            }



            // phan comment
            if (cmtContext != null)
            {
                cuurentComment = new Comment()
                {
                    commentDeadline    = id,
                    comment_DateUpload = DateTime.Now,
                    comment_Content    = cmtContext
                };


                _context.Comments.Add(cuurentComment);
                await _context.SaveChangesAsync();
            }



            var userFile = _context.userFiles.Where(a => a.file_DeadlineId == id);

            userFilesList = userFile.ToList();

            var userComment = _context.Comments.Where(a => a.commentDeadline == id);

            userCommentList = userComment.ToList();

            if (id == null)
            {
                return(NotFound());
            }


            Deadline = await _context.Deadline.FirstOrDefaultAsync(m => m.dl_Id == id);

            if (Deadline == null)
            {
                return(NotFound());
            }
            return(Page());
        }