diff --git a/Controllers/newsController.cs b/Controllers/newsController.cs index 343a4ae..a0562c1 100644 --- a/Controllers/newsController.cs +++ b/Controllers/newsController.cs @@ -2,10 +2,12 @@ using System.Collections.Generic; using System.Data; using System.Data.Entity; +using System.IO; using System.Linq; using System.Net; using System.Web; using System.Web.Mvc; +using System.Web.UI.WebControls; using WebApplication1.Models; namespace WebApplication1.Controllers @@ -40,11 +42,7 @@ namespace WebApplication1.Controllers { if (Session["User"] != null) { - var news = db.news.ToList(); - var cat = db.categories.ToList(); - ViewBag.categoryTitle = cat; - var tags= db.tags.ToList(); - ViewBag.tagTitle = tags; + setLists(); //var models= new Tuple, List>(cat, tags); return View(); } @@ -66,10 +64,49 @@ namespace WebApplication1.Controllers { if (ModelState.IsValid) { + if (Request.Files.Count > 0) + { + setLists(); + var image = Request.Files[0]; + if ( image.ContentLength > 1024 * 1024) + { + ModelState.AddModelError("imageFile", "File size cannot exceed 1MB."); + return RedirectToAction("Index"); + } + + // Check file extension + string fileExtension = Path.GetExtension(image.FileName).ToLower(); + //if (fileExtension != ".jpg" || fileExtension != ".jpeg" || fileExtension != ".png") + if (!new[] { ".jpg", ".jpeg", ".png" }.Contains(fileExtension)) + { + ModelState.AddModelError("image", "Only JPG, JPEG, and PNG files are allowed."); + } + else + { + int length = 10; // Desired length of the random string + string allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + char[] chars = new char[length]; + Random rand = new Random(); + string imgname=""; + for (int i = 0; i < length; i++) + { + chars[i] = allowedChars[rand.Next(0, allowedChars.Length)]; + imgname = "" + chars[i]; + } + + image.SaveAs(Server.MapPath($"~/Media/news/"+imgname+fileExtension)); + } + + + + } + /* + Request.Files[i].SaveAs(Server.MapPath($"~/Media/User/avatar.png")); db.news.Add(news); db.SaveChanges(); return RedirectToAction("Index"); + */ } return View(news); @@ -131,6 +168,14 @@ namespace WebApplication1.Controllers db.SaveChanges(); return RedirectToAction("Index"); } + void setLists() + { + var news = db.news.ToList(); + var cat = db.categories.ToList(); + ViewBag.categoryTitle = cat; + var tags = db.tags.ToList(); + ViewBag.tagTitle = tags; + } protected override void Dispose(bool disposing) { diff --git a/Media/news/H.jpg b/Media/news/H.jpg new file mode 100644 index 0000000..1d98158 Binary files /dev/null and b/Media/news/H.jpg differ diff --git a/Views/news/Create.cshtml b/Views/news/Create.cshtml index 9051af3..d42c0fa 100644 --- a/Views/news/Create.cshtml +++ b/Views/news/Create.cshtml @@ -6,86 +6,86 @@

Create

-@using (Html.BeginForm(new { enctype = "multipart/form-data" })) +@using (Html.BeginForm("Create", "news", FormMethod.Post, new { enctype = "multipart/form-data" })) { @Html.AntiForgeryToken() - -
-

news

-
- @Html.ValidationSummary(true, "", new { @class = "text-danger" }) -
- @Html.LabelFor(model => model.title, htmlAttributes: new { @class = "control-label col-md-2" }) -
- @Html.EditorFor(model => model.title, new { htmlAttributes = new { @class = "form-control" } }) - @Html.ValidationMessageFor(model => model.title, "", new { @class = "text-danger" }) -
-
+
+

news

+
+ @Html.ValidationSummary(true, "", new { @class = "text-danger" }) -
- @Html.LabelFor(model => model.link, htmlAttributes: new { @class = "control-label col-md-2" }) -
- @Html.EditorFor(model => model.link, new { htmlAttributes = new { @class = "form-control" } }) - @Html.ValidationMessageFor(model => model.link, "", new { @class = "text-danger" }) +
+ @Html.LabelFor(model => model.title, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.EditorFor(model => model.title, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.title, "", new { @class = "text-danger" }) +
-
-
- @Html.LabelFor(model => model.image, htmlAttributes: new { @class = "control-label col-md-2" }) -
- @Html.EditorFor(model => model.image, new { htmlAttributes = new { @type = "file", @class = "form-control" } }) - @Html.ValidationMessageFor(model => model.image, "", new { @class = "text-danger" }) +
+ @Html.LabelFor(model => model.link, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.EditorFor(model => model.link, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.link, "", new { @class = "text-danger" }) +
-
-
- -
- - @Html.ValidationMessageFor(model => model.cat, "", new { @class = "text-danger" }) -
-
-
- @Html.LabelFor(model => model.tag, htmlAttributes: new { @class = "control-label col-md-2" }) -
- - @Html.ValidationMessageFor(model => model.tag, "", new { @class = "text-danger" }) +
+ @Html.LabelFor(model => model.image, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.EditorFor(model => model.image, new { htmlAttributes = new { @type = "file", @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.image, "", new { @class = "text-danger" }) +
+
+
+ +
+ + @Html.ValidationMessageFor(model => model.cat, "", new { @class = "text-danger" }) +
-
-
- @Html.LabelFor(model => model.summary, htmlAttributes: new { @class = "control-label col-md-2" }) -
- @Html.TextAreaFor(model => model.summary, new { @class = "form-control" }) - @Html.ValidationMessageFor(model => model.summary, "", new { @class = "text-danger" }) +
+ @Html.LabelFor(model => model.tag, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ + @Html.ValidationMessageFor(model => model.tag, "", new { @class = "text-danger" }) +
-
-
- @Html.LabelFor(model => model.Content, htmlAttributes: new { @class = "control-label col-md-2" }) -
- @Html.TextAreaFor(model => model.Content, new {@class = "form-control"} ) - @Html.ValidationMessageFor(model => model.Content, "", new { @class = "text-danger" }) -
-
-
-
-
- +
+ @Html.LabelFor(model => model.summary, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.TextAreaFor(model => model.summary, new { @class = "form-control" }) + @Html.ValidationMessageFor(model => model.summary, "", new { @class = "text-danger" }) +
+
+
+ @Html.LabelFor(model => model.Content, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.TextAreaFor(model => model.Content, new { @class = "form-control" }) + @Html.ValidationMessageFor(model => model.Content, "", new { @class = "text-danger" }) +
+
+
+ +
+
+ +
-
}
diff --git a/WebApplication1.csproj b/WebApplication1.csproj index b4f5157..c8c5b71 100644 --- a/WebApplication1.csproj +++ b/WebApplication1.csproj @@ -277,6 +277,7 @@ +