前台单击文件,jQuery删除后台相应真实的文件

 Untitled Page

    <script type="text/javascript">
        $(function() {
            $("body img").click(function() {
                var name = $(this).attr("alt");
                $.ajax({
                url: "DeletePicsForm.aspx",
                    data: "picname="+name,
                    datatype: "json",
                    type: "GET",
                    contentType: "application/json; charset=utf-8",
                    success: function(data, textStatus) {
                        alert(data.result);
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        alert(XMLHttpRequest);
                    }
                });
            });
        });
    </script>

DeletePicsForm.aspx.cs:

  protected void Page_Load(object sender, EventArgs e)         {      

       if (Request["picname"] != null)             {    

             Response.Clear();             

           Response.ContentType = "application/json";     

           String result = "success";               

         try                 {          

           File.Delete(Server.MapPath(@"\Images\")+Request["picname"].ToString());       

          }             

       catch (Exception ee)                 {   

           result = ee.Message;       

          }             

        Response.Write("{\"result\":\"" +result+ "\"}");     

            Response.End();             }   

      }

原文地址:https://www.cnblogs.com/dongbo19910728/p/2958374.html