public async Task <IActionResult> UpdatePhoto(StudentPhoto student) { if (student.FileToUpload != null && student.FileToUpload.Length > 0) { try { // Find the filename extension of the file to be uploaded. string fileExt = Path.GetExtension(student.FileToUpload.FileName); // Rename the uploaded file with the staff’s name. string uploadedFile = student.StudentID + fileExt; // Get the complete path to the images folder in server string savePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\images", uploadedFile); // Upload the file to server using (var fileSteam = new FileStream(savePath, FileMode.Create)) { await student.FileToUpload.CopyToAsync(fileSteam); } student.Photo = uploadedFile; studentContext.UpdatePhoto(student); ViewData["Message"] = "File uploaded successfully."; } catch (IOException) { //File IO error, could be due to access rights denied ViewData["Message"] = "File uploading fail!"; } catch (Exception ex) //Other type of error { ViewData["Message"] = ex.Message; } } return(View(student)); }
private string StudentImgSave() { if (StudentPhoto.HasFile) { string fileext = System.IO.Path.GetExtension(StudentPhoto.FileName); if (fileext.ToLower() != ".jpg" && fileext.ToLower() != ".png" && fileext.ToLower() != ".jpeg") { StudPicErrorLbl.Text = "Only file with .jpg , .png or . jpeg are allowed"; return("n"); } else { int filesize = StudentPhoto.PostedFile.ContentLength; if (filesize > 2097152) { StudPicErrorLbl.Text = "File is too big"; return("n"); } else { StudentPhoto.SaveAs(Server.MapPath("~/uploads/studentPic/") + FirstName.Text.Trim() + "STUD" + str_NewID.ToString() + ".jpg"); StudPicErrorLbl.Text = "File uploaded"; return("/uploads/studentPic/" + FirstName.Text.Trim() + "STUD" + str_NewID.ToString() + ".jpg"); } } } else { return("n"); } }
//Uploading student profile photo public ActionResult UpdatePhoto() { // Stop accessing the action if not logged in // or account not in the "Student" role in if ((HttpContext.Session.GetString("Role") == null) || (HttpContext.Session.GetString("Role") != "Student")) { return(RedirectToAction("Index", "Home")); } int studentid = Convert.ToInt32(HttpContext.Session.GetInt32("StudentID")); StudentPhoto studentPhoto = studentContext.GetPhotoDetails(studentid); return(View(studentPhoto)); }
public StudentPhoto GetPhotoDetails(int studentID) { SqlCommand cmd = new SqlCommand("SELECT Name, Photo FROM Student WHERE StudentID = @selectedStudentID", conn); cmd.Parameters.AddWithValue("@selectedStudentID", studentID); //object “cmd” as parameter. SqlDataAdapter da = new SqlDataAdapter(cmd); //Create a DataSet object “result" DataSet studentresult = new DataSet(); //Open a database connection. conn.Open(); //Use DataAdapter to fetch data to a table "StaffDetails" in DataSet. da.Fill(studentresult, "StudentDetails"); //Close the database connection conn.Close(); StudentPhoto studentPhoto = new StudentPhoto(); if (studentresult.Tables["StudentDetails"].Rows.Count > 0) { studentPhoto.StudentID = studentID; DataTable table = studentresult.Tables["StudentDetails"]; if (!DBNull.Value.Equals(table.Rows[0]["Name"])) { studentPhoto.Name = table.Rows[0]["Name"].ToString(); } if (!DBNull.Value.Equals(table.Rows[0]["Photo"])) { studentPhoto.Photo = table.Rows[0]["Photo"].ToString(); } return(studentPhoto); } else { return(null); } }
public int UpdatePhoto(StudentPhoto student) { SqlCommand cmd = new SqlCommand("UPDATE Student SET Name=@name, Photo=@photo" + " WHERE StudentID=@selectedStudentID", conn); cmd.Parameters.AddWithValue("@name", student.Name); if (student.Photo != null) { cmd.Parameters.AddWithValue("@photo", student.Photo); } else { cmd.Parameters.AddWithValue("@photo", DBNull.Value); } cmd.Parameters.AddWithValue("@selectedStudentID", student.StudentID); conn.Open(); int count = cmd.ExecuteNonQuery(); conn.Close(); return(count); }