I have a projrct but i want to add another fields for uploade image and save in a folder and database as well.
Loading
I have a projrct but i want to add another fields for uploade image and save in a folder and database as well.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jobin SRank #886 · 2.4K reputationPosted Jun 3, 2024, 1:33 AM EDT
Hi saheb,
Try this
https://www.c-sharpcorner.com/article/simplifying-file-uploads-with-net-core-7-minimal-apis/
Soheb AhmadRank #9,376 · 207 reputationPosted Jun 3, 2024, 2:04 AM EDT
i build this Peoject using Asp.net MVC in visual studio
Jithu ThomasRank #437 · 5.7K reputationPosted Jun 3, 2024, 1:36 AM EDT
Mr. Soheb
you have to specify which programming language and platform which your project is being build. say example asp.net with c# or asp.net with VB.net or windows application or MVC
Regards,
Jithu ThomasRank #437 · 5.7K reputationPosted Jun 3, 2024, 4:56 AM EDT
Mr. Soheb Ahmad
This can be achieved in simple 7 steps: -
1. Set Up the Model
First, create a model to represent the image data. This includes the path to the image.
// Models/ImageModel.cs namespace YourNamespace.Models { public class ImageModel { public int Id { get; set; } public string ImagePath { get; set; } } }2. Create a Database Context
Set up the database context to interact with the database.
// Data/ApplicationDbContext.cs using System.Data.Entity; using YourNamespace.Models; namespace YourNamespace.Data { public class ApplicationDbContext : DbContext { public DbSet Images { get; set; } } }3. Set Up the Controller
Create a controller to handle the image upload logic.
// Controllers/ImageController.cs using System; using System.IO; using System.Web; using System.Web.Mvc; using YourNamespace.Data; using YourNamespace.Models; namespace YourNamespace.Controllers { public class ImageController : Controller { private readonly ApplicationDbContext _context; public ImageController() { _context = new ApplicationDbContext(); } [HttpGet] public ActionResult Upload() { return View(); } [HttpPost] public ActionResult Upload(HttpPostedFileBase file) { if (file != null && file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); var path = Path.Combine(Server.MapPath("~/Images/"), fileName); file.SaveAs(path); var image = new ImageModel { ImagePath = "/Images/" + fileName }; _context.Images.Add(image); _context.SaveChanges(); return RedirectToAction("UploadSuccess"); } return View(); } public ActionResult UploadSuccess() { return View(); } } }4. Create the Views
Set up the views to upload the image and display success message.
Upload View: @{ ViewBag.Title = "Upload Image"; } Upload Image @using (Html.BeginForm("Upload", "Image", FormMethod.Post, new { enctype = "multipart/form-data" })) { Upload } UploadSuccess View: @{ ViewBag.Title = "Upload Success"; } Upload Successful Your image has been uploaded successfully.5. Configure the Database
Ensure you have the necessary database configurations in Web.config.
6. Create the Images Folder
Ensure you have an Images folder in the root of your project where the images will be saved.
7. Enable Migrations and Update Database
Enable migrations and update the database to create the necessary table.