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.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<category>, List<tag>>(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)
{

BIN
Media/news/H.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 297 KiB

View File

@ -6,11 +6,11 @@
<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()
<div class="form">
<div class="form">
<h4>news</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@ -44,7 +44,7 @@
<select id="cat" name="cat" class="form-control" multiple>
@foreach (var item in ViewBag.categoryTitle)
{
<option value= "@item.title">@item.title</option>
<option value="@item.title">@item.title</option>
}
</select>
@Html.ValidationMessageFor(model => model.cat, "", new { @class = "text-danger" })
@ -74,7 +74,7 @@
<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.TextAreaFor(model => model.Content, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Content, "", new { @class = "text-danger" })
</div>
</div>
@ -85,7 +85,7 @@
<input type="submit" value="Create" class="btn btn-outline-dark" />
</div>
</div>
</div>
</div>
}
<div>

View File

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