Hirdetés
- Xiaomi 15T Pro - a téma nincs lezárva
- Prohardver app (nem hivatalos)
- Miért fárad gyorsabban az iPhone akku, mint az androidos?
- Yettel topik
- Xiaomi 15T - reakció nélkül nincs egyensúly
- Realme GT Master Edition - mestermunka
- Sony Xperia 1 V - kizárólag igényeseknek
- Bemutatkozott a Poco X7 és X7 Pro
- Honor Magic6 Pro - kör közepén számok
- Fotók, videók mobillal
Új hozzászólás Aktív témák
-
joysefke
veterán
válasz
martonx
#8580
üzenetére
Nem, azt még nem próbáltam ki.
Alapvetően én ezt az IFormFile-t akarom validálni, maga a model objektumom (Person) nem túl kritikus, annak a propertijeit az osztálydeklarációban dekoráltam [Required]-del, az a része működik és nekem egyelőre elég is.
Dobtam a mai próbálkozásomat (elnapoltam) a kontrollert visszaállítottam a naiv, nem túl alapos kézi validálásra. Jelenleg így néz ki, csak annyi a célja az IFormFile validálásnak, hogy figyelmetlenségből ne lehessen rossz formátumot feltölteni. Tudom, hogy a fájl kiterjesztése semmit nem jelent.
A Person egy ViewModel objektum és tárolja egy személy adatait illetve a képének a relatív útvonalát a wwwroot-hoz képest. Összesen kb 6-10 Person objektum lesz, ezek in-memory tárolódnak (és minden változás mentődik a diszkre). Ők nem userek, hanem csupán koszorúslányok-fiúk, a képük-nevük pedig a Weblapon fog virítani. Az admin egyszer konfigurálja őket a content-manager oldalon utána ezek úgy maradnak.
A dolog nincsen túlságosan kitesztelve, de működik.
[HttpPost] public IActionResult EditPerson(Person model, int id, IFormFile file)
{
// Text based informations (Person object) and IFormFile gets validated separatelly
// The expectation is to execute EditPerson if the Person is valid, even if IFormFile is missing or invalid
// Exceptions can only be raised bc of file IO errors. This should not break the method
// we collect the exceptions to log details later into ModelState
IList<Exception> exceptions = new List<Exception>();
if (id < 0 || store.Data.People.Length <= id) ModelState.AddModelError("", "Error: route variable \"id\" has invalid value");
if (model?.Title != "Groomsmen" && model?.Title != "Bridesmaid") ModelState.AddModelError("", "Http post request contains invalid title: must be \"Bridesmaid\" or \"Groomsmen\"");
if (ModelState.IsValid)
{ // Person object is valid and updates the ContentStore no matter the IFormFile state
store.Data.People[id] = model;
store.Update(store.Data.People);
} // IFormFile gets manually validated
if (file != null)
{
string name = file.FileName;
string ext = Path.GetExtension(file.FileName.ToLower());
if (1024 * 1024 < file.Length) ModelState.AddModelError("", "Error: A maximum image size of 1MB allowed");
if (ext != ".jpeg" && ext != ".jpg") ModelState.AddModelError("", "Error: JPEG file expected extension must be either *.jpeg or *.jpg");
if (ModelState.IsValid)
{
// currently no renaming and no protection against malicious IFileForm-s as file upload is not available for public
string newFileName = file.FileName;
string newFileFullPath = Path.Combine(webRoot, imageFolder, newFileName);
while (System.IO.File.Exists(newFileFullPath))
{
//gets a nice, short random file name in case of a filename conflict
newFileName = (DateTime.Now.Ticks % 1000).ToString() + ext;
newFileFullPath = Path.Combine(webRoot, imageFolder, newFileName);
}
try
{
using (FileStream fs = System.IO.File.Create(newFileFullPath))
{
file.CopyTo(fs);
fs.Flush();
// file path gets saved in cshtml friendly relative path
store.Data.People[id].PictureSrc = "/" + imageFolder + "/" + newFileName;
}
}
catch (Exception ex) { exceptions.Add(ex); }
}
if (0 < exceptions.Count)
{
ModelState.AddModelError("", "Error: Could not upload image");
foreach (var ex in exceptions) ModelState.AddModelError("", ex.Message);
}
else store.Update(store.Data.People);
}
if (ModelState.IsValid) return RedirectToAction(nameof(Index));
else
{ ViewBag.id = id;
return View(model);
}
}
Új hozzászólás Aktív témák
● ha kódot szúrsz be, használd a PROGRAMKÓD formázási funkciót!
- AMD Navi Radeon™ RX 9xxx sorozat
- Battlefield 6
- Lexus, Toyota topik
- Óra topik
- Konteó topic
- Xiaomi 15T Pro - a téma nincs lezárva
- Gigantikus fordulatot vett a GeForce RTX 50 Super sorozat törlése
- Prohardver app (nem hivatalos)
- HiFi műszaki szemmel - sztereó hangrendszerek
- Miért fárad gyorsabban az iPhone akku, mint az androidos?
- További aktív témák...
- Lenovo IdeaPad Slim 3 16ABR8 Ryzen 5, 16GB RAM, 512GB SSD, dedikált grafika, garanciás
- Hp Zbook Fury G7 " Gyári magyar Bill " Sok - Sok darab!!!!
- Bomba áron GIGABYTE G5 /I7-13620H 16GB 1 TB SSD Nvidia RTX 4050 6GB 144 Hz FHD IPS
- Honor Pad X8 / 4/64GB / Wi-Fi / 12 Hó Garancia
- Samsung Galaxy A16 / 4/128GB / Kártyafüggetlen / 12Hó Garancia
- GYÖNYÖRŰ iPhone 13 128GB Starlight- 1 ÉV GARANCIA, Kártyafüggetlen,MS3435
- HIBÁTLAN iPhone 13 mini 128GB Starlight -1 ÉV GARANCIA -Kártyafüggetlen, MS3614, 100% Akkumulátor
- AKCIÓ!!! RITKASÁG! Microsoft Surface Pro 11 Qualcomm Snapdragon X Elite 16GB 1000GB OLED 120Hz Gar!
- Nuki Smart Lock Ultra okoszár
- HIBÁTLAN iPhone 14 128GB Midnight -1 ÉV GARANCIA - Kártyafüggetlen, MS3240, 100% Akksi
Állásajánlatok
Cég: NetGo.hu Kft.
Város: Gödöllő
Cég: Laptopműhely Bt.
Város: Budapest


