How to Upload Images on Database in SmarterAsp.net and retrieve?

Viewed 19

Answer:

You can give the Server Map of your destination folder Path instead of your domain Url because it will automatically find the folder in your project through your connection string so don't worry about it.

The answer is the same if you are getting an error of not rooted path

        string filename = DateTime.UtcNow.Ticks + ".jpg";
        file.SaveAs(Server.MapPath("~/dbImage/") + filename);

This is the main code that defines the destination path of your image folder after then you can use save file code to save it in the database.

        products.Image = filename;
        db.products.Add(products);
        db.SaveChanges();

Complete Code :

[HttpPost]
        public ActionResult Index(Products products, HttpPostedFileBase file)
        {
            string filename = DateTime.UtcNow.Ticks + ".jpg";
            file.SaveAs(Server.MapPath("~/dbImage/") + filename);
        
            products.Image = filename;
            db.products.Add(products);
            db.SaveChanges();
            return Redirect("/Home/Index");
        }
0 Answers
Related