C
C#11mo ago
ShuajbM

✅ how can i save a photo in my project in .net core

when im trying to add a photo it doesnt show on the screen and form isnt submited?
2 Replies
ShuajbM
ShuajbMOP11mo ago
c#
[HttpPost]
public IActionResult Create(Punet obj,IFormFile? file)
{
if (ModelState.IsValid)
{
string wwwRootPath = _webHostEnvironment.WebRootPath;
if(file != null)
{
string fileName=Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
string punetPath=Path.Combine(wwwRootPath, @"images\fotot");

using (var fileStream = new FileStream(Path.Combine(punetPath, fileName),FileMode.Create))
{
file.CopyTo(fileStream);
}
Console.WriteLine($"ImageUrl: {obj.ImageUrl}");

obj.ImageUrl = @"\images\fotot\" + fileName;

// More debugging
Console.WriteLine($"Updated ImageUrl: {obj.ImageUrl}");

}
_unitOfWork.Punet.Add(obj);
_unitOfWork.Save();
TempData["success"] = "Puna u krijua me sukses";
return RedirectToAction("Index", "Punet");
}
return View();
}
c#
[HttpPost]
public IActionResult Create(Punet obj,IFormFile? file)
{
if (ModelState.IsValid)
{
string wwwRootPath = _webHostEnvironment.WebRootPath;
if(file != null)
{
string fileName=Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
string punetPath=Path.Combine(wwwRootPath, @"images\fotot");

using (var fileStream = new FileStream(Path.Combine(punetPath, fileName),FileMode.Create))
{
file.CopyTo(fileStream);
}
Console.WriteLine($"ImageUrl: {obj.ImageUrl}");

obj.ImageUrl = @"\images\fotot\" + fileName;

// More debugging
Console.WriteLine($"Updated ImageUrl: {obj.ImageUrl}");

}
_unitOfWork.Punet.Add(obj);
_unitOfWork.Save();
TempData["success"] = "Puna u krijua me sukses";
return RedirectToAction("Index", "Punet");
}
return View();
}
c#
@model Punet

<form method="post" enctype="multipart/form-data">
<div class="border p-3 mt-4">
<div class="row pb-2">
<h2 class="text-primary">Krijo nje Pune</h2>
<hr/>
</div>
<div class="mb-3 p-1">
<label asp-for="Name" class="p-0"></label>
<input asp-for="Name" class="form-control"/>
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="mb-3 p-1">
<label asp-for="Description" class="p-0"></label>
<input asp-for="Description" class="form-control" />
<span asp-validation-for="Description" class="text-danger"></span>
</div>
<div class="mb-3 p-1">
<label asp-for="Kerkesat" class="p-0"></label>
<input asp-for="Kerkesat" class="form-control" />
<span asp-validation-for="Kerkesat" class="text-danger"></span>
</div>
<div class="mb-3 p-1">
<label asp-for="Lokacioni" class="p-0"></label>
<input asp-for="Lokacioni" class="form-control" />
<span asp-validation-for="Lokacioni" class="text-danger"></span>
</div>
<div class="mb-3 p-1">
<label asp-for="kategoria" class="p-0"></label>
<input asp-for="kategoria" class="form-control" />
<span asp-validation-for="kategoria" class="text-danger"></span>
</div>

<div class="mb-3 p-1">
<label asp-for="ImageUrl" class="p-0"></label>
<input type="file" name="file" class="form-control" />
</div>
<div class="row">
<div class="col-6">
<button type="submit" class="btn btn-primary form-control" >Krijo</button>
</div>
<div class="col-6">
<a asp-controller="Punet" asp-action="Index" class="btn btn-outline-secondary form-control" >Kthehu</a>
</div>
</div>
</div>
</form>
c#
@model Punet

<form method="post" enctype="multipart/form-data">
<div class="border p-3 mt-4">
<div class="row pb-2">
<h2 class="text-primary">Krijo nje Pune</h2>
<hr/>
</div>
<div class="mb-3 p-1">
<label asp-for="Name" class="p-0"></label>
<input asp-for="Name" class="form-control"/>
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="mb-3 p-1">
<label asp-for="Description" class="p-0"></label>
<input asp-for="Description" class="form-control" />
<span asp-validation-for="Description" class="text-danger"></span>
</div>
<div class="mb-3 p-1">
<label asp-for="Kerkesat" class="p-0"></label>
<input asp-for="Kerkesat" class="form-control" />
<span asp-validation-for="Kerkesat" class="text-danger"></span>
</div>
<div class="mb-3 p-1">
<label asp-for="Lokacioni" class="p-0"></label>
<input asp-for="Lokacioni" class="form-control" />
<span asp-validation-for="Lokacioni" class="text-danger"></span>
</div>
<div class="mb-3 p-1">
<label asp-for="kategoria" class="p-0"></label>
<input asp-for="kategoria" class="form-control" />
<span asp-validation-for="kategoria" class="text-danger"></span>
</div>

<div class="mb-3 p-1">
<label asp-for="ImageUrl" class="p-0"></label>
<input type="file" name="file" class="form-control" />
</div>
<div class="row">
<div class="col-6">
<button type="submit" class="btn btn-primary form-control" >Krijo</button>
</div>
<div class="col-6">
<a asp-controller="Punet" asp-action="Index" class="btn btn-outline-secondary form-control" >Kthehu</a>
</div>
</div>
</div>
</form>
in my wwwroot i have a folder images/fotot then when i try to submit the form it doesnt accept and it repeats itself , next time all fields are filled only image doesnt , where can be the problem
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server