Added ability to upload badges images
This commit is contained in:
@@ -21,8 +21,22 @@ namespace MyWebsite.Areas.Admin.Controllers
|
|||||||
View(Startup.Database.Badges.Find(id));
|
View(Startup.Database.Badges.Find(id));
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public IActionResult Edit(Badge model)
|
public IActionResult Edit(Badge model, IFormFile file = null)
|
||||||
{
|
{
|
||||||
|
if(file != null)
|
||||||
|
{
|
||||||
|
System.Drawing.Image image = System.Drawing.Image.FromStream(file.OpenReadStream());
|
||||||
|
if (image.Width != 64 || image.Height != 64 || !file.FileName.ToLower().EndsWith(".png"))
|
||||||
|
{
|
||||||
|
ViewData["UploadException"] = "error";
|
||||||
|
return View(Startup.Database.Badges.Find(model.Name));
|
||||||
|
}
|
||||||
|
using (var stream = System.IO.File.Create(Directory.GetCurrentDirectory() + "/wwwroot/images/Badges/" + file.FileName))
|
||||||
|
file.CopyTo(stream);
|
||||||
|
|
||||||
|
return Redirect(Request.Path.Value);
|
||||||
|
}
|
||||||
|
|
||||||
Startup.Database.Badges.Update(model);
|
Startup.Database.Badges.Update(model);
|
||||||
Startup.Database.SaveChanges();
|
Startup.Database.SaveChanges();
|
||||||
|
|
||||||
@@ -47,8 +61,22 @@ namespace MyWebsite.Areas.Admin.Controllers
|
|||||||
View();
|
View();
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public IActionResult Create(Badge model)
|
public IActionResult Create(Badge model, IFormFile file = null)
|
||||||
{
|
{
|
||||||
|
if (file != null)
|
||||||
|
{
|
||||||
|
System.Drawing.Image image = System.Drawing.Image.FromStream(file.OpenReadStream());
|
||||||
|
if (image.Width != 64 || image.Height != 64 || !file.FileName.ToLower().EndsWith(".png"))
|
||||||
|
{
|
||||||
|
ViewData["UploadException"] = "error";
|
||||||
|
return View(Startup.Database.Badges.Find(model.Name));
|
||||||
|
}
|
||||||
|
using (var stream = System.IO.File.Create(Directory.GetCurrentDirectory() + "/wwwroot/images/Badges/" + file.FileName))
|
||||||
|
file.CopyTo(stream);
|
||||||
|
|
||||||
|
return Redirect(Request.Path.Value);
|
||||||
|
}
|
||||||
|
|
||||||
if (!ModelState.IsValid)
|
if (!ModelState.IsValid)
|
||||||
{
|
{
|
||||||
ModelState.AddModelError("Error", "Invalid data");
|
ModelState.AddModelError("Error", "Invalid data");
|
||||||
|
|||||||
@@ -45,6 +45,13 @@
|
|||||||
|
|
||||||
<input type="submit" value="Create" class="btn" />
|
<input type="submit" value="Create" class="btn" />
|
||||||
</form>
|
</form>
|
||||||
|
<hr />
|
||||||
|
<h2>Upload badge image</h2>
|
||||||
|
<form method="post" enctype="multipart/form-data">
|
||||||
|
<input type="file" accept="image/png" name="file" />
|
||||||
|
<label for="file" style="@(ViewData.Keys.Contains("UploadException") ? "color: red" : "")">File must be a 64x64 PNG image</label>
|
||||||
|
<input type="submit" value="Upload image" class="btn" />
|
||||||
|
</form>
|
||||||
</article>
|
</article>
|
||||||
|
|
||||||
<link type="text/css" rel="stylesheet" href="~/css/Admin.css" />
|
<link type="text/css" rel="stylesheet" href="~/css/Admin.css" />
|
||||||
@@ -45,6 +45,13 @@
|
|||||||
|
|
||||||
<input type="submit" value="Save" class="btn" />
|
<input type="submit" value="Save" class="btn" />
|
||||||
</form>
|
</form>
|
||||||
|
<hr />
|
||||||
|
<h2>Upload badge image</h2>
|
||||||
|
<form method="post" enctype="multipart/form-data">
|
||||||
|
<input type="file" accept="image/png" name="file" />
|
||||||
|
<label for="file" style="@(ViewData.Keys.Contains("UploadException") ? "color: red" : "")">File must be a 64x64 PNG image</label>
|
||||||
|
<input type="submit" value="Upload image" class="btn"/>
|
||||||
|
</form>
|
||||||
</article>
|
</article>
|
||||||
|
|
||||||
<link type="text/css" rel="stylesheet" href="~/css/Admin.css" />
|
<link type="text/css" rel="stylesheet" href="~/css/Admin.css" />
|
||||||
Reference in New Issue
Block a user