saveimages in folder

This commit is contained in:
2024-07-30 13:08:09 +03:30
parent b68d3f3185
commit 323910fb41
4 changed files with 117 additions and 71 deletions

View File

@ -2,10 +2,12 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using System.Data.Entity; using System.Data.Entity;
using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Web; using System.Web;
using System.Web.Mvc; using System.Web.Mvc;
using System.Web.UI.WebControls;
using WebApplication1.Models; using WebApplication1.Models;
namespace WebApplication1.Controllers namespace WebApplication1.Controllers
@ -40,11 +42,7 @@ namespace WebApplication1.Controllers
{ {
if (Session["User"] != null) if (Session["User"] != null)
{ {
var news = db.news.ToList(); setLists();
var cat = db.categories.ToList();
ViewBag.categoryTitle = cat;
var tags= db.tags.ToList();
ViewBag.tagTitle = tags;
//var models= new Tuple<List<category>, List<tag>>(cat, tags); //var models= new Tuple<List<category>, List<tag>>(cat, tags);
return View(); return View();
} }
@ -66,10 +64,49 @@ namespace WebApplication1.Controllers
{ {
if (ModelState.IsValid) 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.news.Add(news);
db.SaveChanges(); db.SaveChanges();
return RedirectToAction("Index"); return RedirectToAction("Index");
*/
} }
return View(news); return View(news);
@ -131,6 +168,14 @@ namespace WebApplication1.Controllers
db.SaveChanges(); db.SaveChanges();
return RedirectToAction("Index"); 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) protected override void Dispose(bool disposing)
{ {

BIN
Media/news/H.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 297 KiB

View File

@ -6,86 +6,86 @@
<h2>Create</h2> <h2>Create</h2>
@using (Html.BeginForm(new { enctype = "multipart/form-data" })) @using (Html.BeginForm("Create", "news", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ {
@Html.AntiForgeryToken() @Html.AntiForgeryToken()
<div class="form">
<h4>news</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group"> <div class="form">
@Html.LabelFor(model => model.title, htmlAttributes: new { @class = "control-label col-md-2" }) <h4>news</h4>
<div class="col-md-10"> <hr />
@Html.EditorFor(model => model.title, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.ValidationMessageFor(model => model.title, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group"> <div class="form-group">
@Html.LabelFor(model => model.link, htmlAttributes: new { @class = "control-label col-md-2" }) @Html.LabelFor(model => model.title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10"> <div class="col-md-10">
@Html.EditorFor(model => model.link, new { htmlAttributes = new { @class = "form-control" } }) @Html.EditorFor(model => model.title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.link, "", new { @class = "text-danger" }) @Html.ValidationMessageFor(model => model.title, "", new { @class = "text-danger" })
</div>
</div> </div>
</div>
<div class="form-group"> <div class="form-group">
@Html.LabelFor(model => model.image, htmlAttributes: new { @class = "control-label col-md-2" }) @Html.LabelFor(model => model.link, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10"> <div class="col-md-10">
@Html.EditorFor(model => model.image, new { htmlAttributes = new { @type = "file", @class = "form-control" } }) @Html.EditorFor(model => model.link, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.image, "", new { @class = "text-danger" }) @Html.ValidationMessageFor(model => model.link, "", new { @class = "text-danger" })
</div>
</div> </div>
</div>
<div class="form-group">
<label class="control-label col-md-2">category</label>
<div>
<select id="cat" name="cat" class="form-control" multiple>
@foreach (var item in ViewBag.categoryTitle)
{
<option value= "@item.title">@item.title</option>
}
</select>
@Html.ValidationMessageFor(model => model.cat, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group"> <div class="form-group">
@Html.LabelFor(model => model.tag, htmlAttributes: new { @class = "control-label col-md-2" }) @Html.LabelFor(model => model.image, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10"> <div class="col-md-10">
<select id="cat" name="cat" class="form-control" multiple> @Html.EditorFor(model => model.image, new { htmlAttributes = new { @type = "file", @class = "form-control" } })
@foreach (var item in ViewBag.tagTitle) @Html.ValidationMessageFor(model => model.image, "", new { @class = "text-danger" })
{ </div>
<option value="@item.title">@item.title</option> </div>
} <div class="form-group">
</select> <label class="control-label col-md-2">category</label>
@Html.ValidationMessageFor(model => model.tag, "", new { @class = "text-danger" }) <div>
<select id="cat" name="cat" class="form-control" multiple>
@foreach (var item in ViewBag.categoryTitle)
{
<option value="@item.title">@item.title</option>
}
</select>
@Html.ValidationMessageFor(model => model.cat, "", new { @class = "text-danger" })
</div>
</div> </div>
</div>
<div class="form-group"> <div class="form-group">
@Html.LabelFor(model => model.summary, htmlAttributes: new { @class = "control-label col-md-2" }) @Html.LabelFor(model => model.tag, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10"> <div class="col-md-10">
@Html.TextAreaFor(model => model.summary, new { @class = "form-control" }) <select id="cat" name="cat" class="form-control" multiple>
@Html.ValidationMessageFor(model => model.summary, "", new { @class = "text-danger" }) @foreach (var item in ViewBag.tagTitle)
{
<option value="@item.title">@item.title</option>
}
</select>
@Html.ValidationMessageFor(model => model.tag, "", new { @class = "text-danger" })
</div>
</div> </div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Content, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.Content, new {@class = "form-control"} )
@Html.ValidationMessageFor(model => model.Content, "", new { @class = "text-danger" })
</div>
</div>
<br />
<div class="form-group"> <div class="form-group">
<div class="col-md-offset-2 col-md-10"> @Html.LabelFor(model => model.summary, htmlAttributes: new { @class = "control-label col-md-2" })
<input type="submit" value="Create" class="btn btn-outline-dark" /> <div class="col-md-10">
@Html.TextAreaFor(model => model.summary, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.summary, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Content, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.Content, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Content, "", new { @class = "text-danger" })
</div>
</div>
<br />
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-outline-dark" />
</div>
</div> </div>
</div> </div>
</div>
} }
<div> <div>

View File

@ -277,6 +277,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="App_Data\" /> <Folder Include="App_Data\" />
<Folder Include="Media\news\" />
<Folder Include="Views\Default\" /> <Folder Include="Views\Default\" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>