How to delete a specific file from folder using asp.net

后端 未结 5 614
感情败类
感情败类 2021-02-01 21:12

here\'s the deal I got a datagridviewer which is called gridview1 and a fileupload1 when i upload a file it updates the gridview1 and table in database with the file name and pa

5条回答
  •  离开以前
    2021-02-01 21:30

    In my project i am using ajax and i create a web method in my code behind like this

    in front

     $("#attachedfiles a").live("click", function () {
                var row = $(this).closest("tr");
                var fileName = $("td", row).eq(0).html();
                $.ajax({
                    type: "POST",
                    url: "SendEmail.aspx/RemoveFile",
                    data: '{fileName: "' + fileName + '" }',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function () { },
                    failure: function (response) {
                        alert(response.d);
                    }
                });
                row.remove();
            });  
    

    in code behind

            [WebMethod]
            public static void RemoveFile(string fileName)
            {
                List files = (List)HttpContext.Current.Session["Files"];
                files.RemoveAll(f => f.FileName.ToLower().EndsWith(fileName.ToLower()));
    
                if (System.IO.File.Exists(HttpContext.Current.Server.MapPath("~/Employee/uploads/" + fileName)))
                {
                    System.IO.File.Delete(HttpContext.Current.Server.MapPath("~/Employee/uploads/" + fileName));
                }
            }
    

    i think this will help you.

提交回复
热议问题