Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

asp.net mvc image upload

 [HttpGet]
        public ActionResult Add_Category()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Add_Category(tbl_category cat , HttpPostedFileBase file)
        {
            string path = uploadimage(file);
            if (path.Equals("-1"))
            {
                ViewBag.error = "Image could not be uploaded";
            }
            else
            {
                tbl_category ca = new tbl_category();

                ca.cat_name = cat.cat_name;
                ca.cat_status = cat.cat_status;
                ca.cat_image = path;
                ca.ad_id_fk = Convert.ToInt32(Session["ad_id"].ToString());

                db.tbl_category.Add(ca);
                db.SaveChanges();

                return RedirectToAction("ViewCategory");
            }
            return View();
          
          //upload image
          
          public string uploadimage(HttpPostedFileBase file)
        {
            Random r = new Random();

            string path = "-1";

            int random = r.Next();

            if (file != null && file.ContentLength > 0)
            {
                string extension = Path.GetExtension(file.FileName);

                if (extension.ToLower().Equals(".jpg") || extension.ToLower().Equals(".jpeg") || extension.ToLower().Equals(".png"))
                {

                    try
                    {
                        path = Path.Combine(Server.MapPath("~/Content/upload/"), random + Path.GetFileName(file.FileName));

                        file.SaveAs(path);

                        path = "~/Content/upload/" + random + Path.GetFileName(file.FileName);
                    }

                    catch (Exception ex)
                    {
                        path = "-1";

                    }

                }
                else
                {
                    Response.Write("<script>alert('ONLY JPG,JPEG AND PNG  Formats are Acceptable'); </script>");
                }


            }
            else
            {
                Response.Write("<script>alert('Please Select a File'); </script>");

                path = "-1";
            }
            return path;

        
          
          
          
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# get dictionary first key 
Csharp :: copy class c# 
Csharp :: c# binding add combobox with enum values 
Csharp :: datetime empty date 
Csharp :: use raycast unity new input system 
Csharp :: .net mvc return a specific View 
Csharp :: how to convert object in string JSON c# 
Csharp :: socket io connect to namespace 
Csharp :: How to find out if a file exists in C# / .NET? 
Csharp :: get file path in .net core from wwwroot folder 
Csharp :: c# generate guid from hash 
Csharp :: vb.net add row to datagridview programmatically 
Csharp :: c# how to set string list 
Csharp :: c# close program 
Csharp :: c# array display 
Csharp :: how to get unique list in c# 
Csharp :: how to get file type from base64 in c# 
Csharp :: How to type custom backcolor on c# winform 
Csharp :: what is unity 
Csharp :: get last index C# 
Csharp :: unity detect a touch on ui element 
Csharp :: asp.net core 6 autofac 
Csharp :: Get Last Access Time Of Directory C# 
Csharp :: unity button not working 
Csharp :: c# loop class properties add to array 
Csharp :: return an interface or a class C# 
Csharp :: c# linq list select 
Csharp :: and operator in c# 
Csharp :: serilog .net 6 
Csharp :: csharp csvhelper 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =