Я хочу загрузить изображение профиля ⇐ C#
-
Anonymous
Я хочу загрузить изображение профиля
I want to create a new user using a CRUD Operation, with some information and a profile picture, but when i try to upload the picture it says that it is invalid
This is my model for the create page
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Humanizer.Bytes; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.Rendering; using Task_CRM.Data; using Task_CRM.Models; namespace Task_CRM.Pages.UserPage { public class CreateModel : PageModel { private readonly Task_CRM.Data.Task_CRMContext _context; private readonly IWebHostEnvironment _environment; public string? Username { get; set; } public CreateModel(Task_CRM.Data.Task_CRMContext context, IWebHostEnvironment environment) { _context = context; _environment = environment; } public IActionResult OnGet() { Username = HttpContext.Session.GetString("Username"); if (Username != null) { ViewData["Navbar"] = Username; } ViewData["RoleID"] = new SelectList(_context.Role, "Id", "RoleType"); return Page(); } [BindProperty] public User User { get; set; } = default!; public bool ? IsAdmin { get; set; } // To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD public async Task OnPostAsync(IFormFile uploadFiles, User img) { var currentUser = HttpContext.Session.GetString("Username"); if (currentUser != null) { var findUser = _context.User.FirstOrDefault(u => u.Name == currentUser); if(findUser == null) { return Page(); } int RoleID = findUser.RoleID; // if the customer is sales return to page if(RoleID == 1) { IsAdmin = false; return Page(); } } if(uploadFiles != null) { string imgext = Path.GetExtension(uploadFiles.FileName); if (imgext == ".jpg" || imgext == ".png") { var imgsave = Path.Combine(_environment.WebRootPath, "ProfilePicture", uploadFiles.FileName); var stream = new FileStream(imgsave, FileMode.Create); await uploadFiles.CopyToAsync(stream); stream.Close(); img.Name = uploadFiles.FileName; img.ProfilePicturePath = imgsave; await _context.User.AddAsync(img); await _context.SaveChangesAsync(); } } if (!ModelState.IsValid) { return Page(); } _context.User.Add(User); await _context.SaveChangesAsync(); return RedirectToPage("./Index"); } } } This is my page
@page @model Task_CRM.Pages.UserPage.CreateModel @{ ViewData["Title"] = "Create"; } Create User @if (@Model.IsAdmin == false) { Only Admin is Able to Create new User!!!
} Back to List @section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} }
This is the User Model that i use
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace Task_CRM.Models { public class User { public int Id { get; set; } [Required(ErrorMessage = "Nama tidak boleh kosong")] [MinLength(5, ErrorMessage = "Nama tidak boleh kurang dari 5")] public string? Name { get; set; } [Required(ErrorMessage = "Email tidak boleh kosong")] [EmailAddress] public string? Email { get; set; } [Required] [MinLength(5, ErrorMessage = "Password tidak boleh kurang dari 5")] public string? Password { get; set; } [Required(ErrorMessage = "No Telepon tidak boleh kosong")] [Phone] public string? Phone { get; set; } public int RoleID { get; set; } public Role? Role { get; set; } public string ProfilePicturePath { get; set; } public ICollection? Interactions { get; set; } } } i try to put this line of codes, but i don't think it works
if(uploadFiles != null) { string imgext = Path.GetExtension(uploadFiles.FileName); if (imgext == ".jpg" || imgext == ".png") { var imgsave = Path.Combine(_environment.WebRootPath, "ProfilePicture", uploadFiles.FileName); var stream = new FileStream(imgsave, FileMode.Create); await uploadFiles.CopyToAsync(stream); stream.Close(); img.Name = uploadFiles.FileName; img.ProfilePicturePath = imgsave; await _context.User.AddAsync(img); await _context.SaveChangesAsync(); } }
Источник: https://stackoverflow.com/questions/781 ... le-picture
I want to create a new user using a CRUD Operation, with some information and a profile picture, but when i try to upload the picture it says that it is invalid
This is my model for the create page
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Humanizer.Bytes; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.Rendering; using Task_CRM.Data; using Task_CRM.Models; namespace Task_CRM.Pages.UserPage { public class CreateModel : PageModel { private readonly Task_CRM.Data.Task_CRMContext _context; private readonly IWebHostEnvironment _environment; public string? Username { get; set; } public CreateModel(Task_CRM.Data.Task_CRMContext context, IWebHostEnvironment environment) { _context = context; _environment = environment; } public IActionResult OnGet() { Username = HttpContext.Session.GetString("Username"); if (Username != null) { ViewData["Navbar"] = Username; } ViewData["RoleID"] = new SelectList(_context.Role, "Id", "RoleType"); return Page(); } [BindProperty] public User User { get; set; } = default!; public bool ? IsAdmin { get; set; } // To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD public async Task OnPostAsync(IFormFile uploadFiles, User img) { var currentUser = HttpContext.Session.GetString("Username"); if (currentUser != null) { var findUser = _context.User.FirstOrDefault(u => u.Name == currentUser); if(findUser == null) { return Page(); } int RoleID = findUser.RoleID; // if the customer is sales return to page if(RoleID == 1) { IsAdmin = false; return Page(); } } if(uploadFiles != null) { string imgext = Path.GetExtension(uploadFiles.FileName); if (imgext == ".jpg" || imgext == ".png") { var imgsave = Path.Combine(_environment.WebRootPath, "ProfilePicture", uploadFiles.FileName); var stream = new FileStream(imgsave, FileMode.Create); await uploadFiles.CopyToAsync(stream); stream.Close(); img.Name = uploadFiles.FileName; img.ProfilePicturePath = imgsave; await _context.User.AddAsync(img); await _context.SaveChangesAsync(); } } if (!ModelState.IsValid) { return Page(); } _context.User.Add(User); await _context.SaveChangesAsync(); return RedirectToPage("./Index"); } } } This is my page
@page @model Task_CRM.Pages.UserPage.CreateModel @{ ViewData["Title"] = "Create"; } Create User @if (@Model.IsAdmin == false) { Only Admin is Able to Create new User!!!
} Back to List @section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} }
This is the User Model that i use
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace Task_CRM.Models { public class User { public int Id { get; set; } [Required(ErrorMessage = "Nama tidak boleh kosong")] [MinLength(5, ErrorMessage = "Nama tidak boleh kurang dari 5")] public string? Name { get; set; } [Required(ErrorMessage = "Email tidak boleh kosong")] [EmailAddress] public string? Email { get; set; } [Required] [MinLength(5, ErrorMessage = "Password tidak boleh kurang dari 5")] public string? Password { get; set; } [Required(ErrorMessage = "No Telepon tidak boleh kosong")] [Phone] public string? Phone { get; set; } public int RoleID { get; set; } public Role? Role { get; set; } public string ProfilePicturePath { get; set; } public ICollection? Interactions { get; set; } } } i try to put this line of codes, but i don't think it works
if(uploadFiles != null) { string imgext = Path.GetExtension(uploadFiles.FileName); if (imgext == ".jpg" || imgext == ".png") { var imgsave = Path.Combine(_environment.WebRootPath, "ProfilePicture", uploadFiles.FileName); var stream = new FileStream(imgsave, FileMode.Create); await uploadFiles.CopyToAsync(stream); stream.Close(); img.Name = uploadFiles.FileName; img.ProfilePicturePath = imgsave; await _context.User.AddAsync(img); await _context.SaveChangesAsync(); } }
Источник: https://stackoverflow.com/questions/781 ... le-picture
Мобильная версия