Thursday, 9 January 2020

Delete File C# ASP.Net

JS



function DeleteImage(path)
{
    if (confirm('Are you sure to delete this Image')) {

        var Userobj =
           {

           }

        $.ajax({

            url: '/WebsiteAdmin/DeleteAssociateImage?path=' + path,
            type: "POST",
            contentType: false, // Not to set any content header 
            processData: false, // Not to process data 
            data: JSON.stringify(Userobj),

            success: function (result) {
                //Update Extension
                if (result.sMessage == "1") {
                    alert("Image deleted Successfully");
                    location.reload();


                }
                else
                    alert(result.sMessage);

            },
            error: function (abc) {
                alert(abc.statusText);

            }
        });
    }

}


C# Code



[HttpPost]
        public JsonResult DeleteAssociateImage(string path, string type)
        {         
            try
            {
                string filename = Server.MapPath(path);
                if (System.IO. File.Exists(filename))
                {
                    System.IO.File.Delete(filename);
                }
             
                return Json(new { sMessage = "1", JsonRequestBehavior.AllowGet });
            }
            catch(Exception ex)
            {
                return Json(new { sMessage = ex.Message, JsonRequestBehavior.AllowGet });
            }
        }

No comments:

Post a Comment