1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> AjoutArticle([Bind("Id,Nom,Description,ArticleRefVendeur,Categorie,SelectedCategorie,SelectedSousCategorie,Matieres,Couleurs,Circa,Style,Pays,Hauteur,Largeur,Profondeur,Diametre,Poids,Designer,NombreLot,NombreUniteDansLot,NombreUnite,EtatArticle,PrixPublic,Negociable,PrixReserve,PrixSolde,DateDebutSolde,DateFinSolde,ParticipationLivraison,CodeLivraisonClasse,LivraisonParVendeur,DateEntree,DateSortie,TauxCommission,DemandeInventaire,Reserver,CodeStatut,Visible,CodeModeration,NotesModeration,PrixPropositionLC,CodePromotion,Crec_Ref,ApplicationUserId")] Articles article)
{
if (ModelState.IsValid)
{
var files = HttpContext.Request.Form.Files;
string extension = Path.GetExtension(files[0].FileName);
string monfilename = Guid.NewGuid().ToString() + extension;
var RepUser = HttpContext.User.Identity.Name;
var RepItem = article.Crecidan_Ref;
// Rename
char[] Alphab = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
monfilename = monfilename.Replace(monfilename[0], Alphab[0]);
// F U L L I M A G E
string UploadFolder = "wwwroot\\upload\\" + RepUser + "\\" + RepItem;
string pathString = Path.Combine(Directory.GetCurrentDirectory(), UploadFolder);
string UrlImage = UploadFolder + "\\" + monfilename;
// T H U M B N A I L S
string THUMB_UploadFolder = "wwwroot\\upload\\" + RepUser + "\\" + RepItem + "\\Thumbnails";
string THUMB_pathString = Path.Combine(Directory.GetCurrentDirectory(), THUMB_UploadFolder);
string THUMB_UrlImage = THUMB_UploadFolder + "\\" + monfilename;
article.VendeurName = RepUser;
article.FirstImage = monfilename;
article.CheminFirstImage = THUMB_pathString;
article.UrlImage = Path.Combine(Directory.GetCurrentDirectory(), THUMB_UrlImage);
_repo.CreateArticle(article);
await _repo.SaveChangeAsync(article);
Images result = new Images();
int i = 0;
foreach (IFormFile monfile in files)
{
if (monfile != null && monfile.Length > 0)
{
if (i != 0)
{
extension = Path.GetExtension(monfile.FileName);
monfilename = Guid.NewGuid().ToString() + extension;
monfilename = monfilename.Replace(monfilename[0], Alphab[i]);
THUMB_UrlImage = THUMB_UploadFolder + "\\" + monfilename;
}
// ---- I M A G E S ----
Images image = new Images();
image.Nom = monfilename;
image.Url = Path.Combine(Directory.GetCurrentDirectory(), UrlImage);
image.ArticlesId = article.Id;
_IMG_repo.CreateImage(image);
await _IMG_repo.SaveChangeIMGAsync(image);
bool exists = System.IO.Directory.Exists(pathString);
if (!exists) System.IO.Directory.CreateDirectory(pathString);
bool Thumb_exists = System.IO.Directory.Exists(THUMB_pathString);
if (!Thumb_exists) System.IO.Directory.CreateDirectory(THUMB_pathString);
// T H U M B N A I L S
MemoryStream target = new MemoryStream();
files[i].CopyTo(target);
byte[] data = target.ToArray();
using var photo = new MagickImage(data);
var size = new MagickGeometry(250, 250);
size.IgnoreAspectRatio = false;
photo.Resize(size);
// Save the result
photo.Write(THUMB_UrlImage, MagickFormat.Jpg);
// --------------------
i++;
// F U L L I M A G E
using FileStream stream = new FileStream(Path.Combine(pathString, monfilename), FileMode.Create);
monfile.CopyTo(stream);
result.Nom = monfilename;
result.Url = UploadFolder + "/" + monfilename;
}
}
return RedirectToAction("MesArticles","Articles");
}
ViewData["ApplicationUserId"] = _repo.GetUserWithArticle(article);
ViewBag.CategoryId = TrouveCategorie().Select(c => new SelectListItem { Value = c.Id.ToString(), Text = c.Nom }).ToList();
return View(article);
} |