public async Task OnPost() { // For more information check the docs at: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-2.2 using (var newFile = new FileStream(Path.Combine(HostInformation.WebRootPath, @"images", UploadFile.FileName), FileMode.CreateNew)) { await UploadFile.CopyToAsync(newFile); } }
public async Task <IActionResult> OnPostUpload() { fileManager = new FileManager(); using (var fileStream = fileManager.GetFileStream()) { await UploadFile.CopyToAsync(fileStream); } FileUpload = fileManager.IdentifyFile(); return(Page()); }
// To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { if (UploadFile != null) { if (UploadFile.Length > 0) { var fileName = Guid.NewGuid().ToString() + Path.GetExtension(UploadFile.FileName); var dir = Path.Combine(hostingEnvironment.ContentRootPath, "wwwroot/imageUpload"); Notice.NoticeImagePath = fileName; var filePath = Path.Combine(dir, fileName); using (var stream = System.IO.File.Create(filePath)) { await UploadFile.CopyToAsync(stream); } } } else { return(Page()); } //Set the date enable bit so that the model is still valid Notice.NoticeStartDate = DateTime.Now; Notice.NoticeStopDate = DateTime.Now; Notice.EnabelStopDate = false; if (!ModelState.IsValid) { return(Page()); } _context.Notices.Add(Notice); await _context.SaveChangesAsync(); NoticeAssignments = new List <NoticeAssignment>(); foreach (var display in checkedDisplays) { NoticeAssignment assignment = new NoticeAssignment(); assignment.NoticeID = Notice.NoticeID; assignment.NoticeDisplayID = display; NoticeAssignments.Add(assignment); } _context.NoticeAssignments.AddRange(NoticeAssignments); await _context.SaveChangesAsync(); await _hubContext.Clients.All.SendAsync("Reload"); return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int?id) { UserRec = await _context.User.FirstOrDefaultAsync(m => m.ID == id); Boolean check = CheckPic(UserRec.FirstName, UserRec.EmailAdd); if (!check) { //Saving the file to the server var Fileupload = Path.Combine(_env.WebRootPath, "Images", UploadFile.FileName); Console.WriteLine(Fileupload); using (var Fstream = new FileStream(Fileupload, FileMode.Create)) { await UploadFile.CopyToAsync(Fstream); ViewData["Message"] = "File Uploaded to Image Data folder"; } Console.WriteLine("Email is -->" + UserRec.EmailAdd); Console.WriteLine("File Name is -->" + UploadFile.FileName); Console.WriteLine("First Name is -->" + UserRec.FirstName); var connectionStringBuilder = new SqliteConnectionStringBuilder(); DatabaseConnect DBCon = new DatabaseConnect(); // your own class and method in DatabaseConnection folder string dbStringConnection = DBCon.DBStringConnection(); connectionStringBuilder.DataSource = dbStringConnection; var connection = new SqliteConnection(connectionStringBuilder.ConnectionString); connection.Open(); var selectCmd2 = connection.CreateCommand(); selectCmd2.CommandText = @"INSERT INTO Picture (Email, PicName, FirstName) VALUES ($email, $PicName, $firstName)"; selectCmd2.Parameters.AddWithValue("$email", UserRec.EmailAdd); selectCmd2.Parameters.AddWithValue("$PicName", UploadFile.FileName); selectCmd2.Parameters.AddWithValue("$firstName", UserRec.FirstName); selectCmd2.Prepare(); selectCmd2.ExecuteNonQuery(); return(RedirectToPage("/AdminPage/UserDetails")); } else { ViewData["Message"] = "The user already has a picture. Go to update profile."; return(Page()); } }
public async Task <IActionResult> OnPostUploadAsync() { if (UploadFile != null) { if (!_fs.ValidateFile(UploadFile)) { ModelState.TryAddModelError("UploadFile_Type", "The file is not of an allowed type."); } // Is image less the 2MB? // TODO: Parameterized size if (UploadFile.Length > 2097152) { ModelState.TryAddModelError("UploadFile_Size", "The file size is too big."); } } else { ModelState.TryAddModelError("UploadFIle_Empty", "There is no file selected for upload."); } if (!ModelState.IsValid) { return(Page()); } // Save uploaded file using (var ms = new MemoryStream()) { await UploadFile.CopyToAsync(ms); // Is file less then 2 MB? // TODO: Parameterize length. in AppSettings.json or configurable through databse? // IS this check really necessary if we check the length above? if (ms.Length <= 2097152) { await _fs.SaveFile(ms.ToArray(), UploadFile.FileName); } } SuccessShow = true; SuccessMessage = "File uploaded successfully!"; return(Page()); }
public async Task <IActionResult> OnPostAsync() { string username = HttpContext.Session.GetString("username"); User user = await _context.User.FirstOrDefaultAsync(u => u.UserName.Equals(username)); AppCondition app = await _context.AppCondition.FirstOrDefaultAsync(app => app.AppConditionId.Equals(1)); Team = await _context.Team.FirstOrDefaultAsync(t => t.UserID.Equals(user.UserId) && t.EventID.Equals(app.EventID)); //The team won't be null because of test performed earlier if ((UploadFile == null) || (UploadFile.Length == 0)) { Message = "Please select a file first to upload!!"; return(Page()); } //Before we store the file we need to get rid of the previous file if any TeamPresentation teamPresentation1 = await _context.TeamPresentation.FirstOrDefaultAsync(tp => tp.TeamID.Equals(Team.TeamId) && tp.EventID.Equals(app.EventID)); if (teamPresentation1 != null) { //That means team leader has already upload a file before this //So we need to start by deleting that file first string storedFile = teamPresentation1.FileName; var oldPath = Path.Combine(Directory.GetCurrentDirectory(), "Files", storedFile); if (System.IO.File.Exists(oldPath)) { System.IO.File.Delete(oldPath); _context.TeamPresentation.Remove(teamPresentation1); await _context.SaveChangesAsync(); Message = "Your previous file has been deleted!!"; } else { } } string extension = Path.GetExtension(UploadFile.FileName); //Since team name is unique for an event and username is unique always string fileName = Team.JoinCode + username + Team.TeamId + extension; var path = Path.Combine( Directory.GetCurrentDirectory(), "Files", fileName); using (var stream = new FileStream(path, FileMode.Create)) { await UploadFile.CopyToAsync(stream); } Message = "You file has been uploaded!!"; TeamPresentation teamPresentation = new TeamPresentation() { TeamID = Team.TeamId, EventID = Team.EventID, FileName = fileName, TeamName = Team.TeamName, }; _context.TeamPresentation.Add(teamPresentation); await _context.SaveChangesAsync(); return(RedirectToPage("./MyTeam")); }