点击图片,AJAX删除后台图片文件的实现代码(asp.net)
程序员文章站
2024-03-08 10:07:04
包含了2个页面,一个是显示图片的页面,一个是传递文件名,然后删除真实图片的页面。具体的代码如下: showpics.htm: 复制代码 代码如下:
包含了2个页面,一个是显示图片的页面,一个是传递文件名,然后删除真实图片的页面。具体的代码如下:
showpics.htm:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>untitled page</title>
<script src="js/jquery-1.4.4.js" type="text/javascript"></script>
<script src="js/json2.js" type="text/javascript"></script>
<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>
</head>
<body>
<div>
<img src="images/xiyangyang.jpg" alt="xiyangyang.jpg" />
</div>
</body>
</html>
具体的删除的页面的代码如下:
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();
}
}
对于上面图片名称的传递,是用的get方式,想换成post方式可以用如下的方法:
$(function() {
$("body img").click(function() {
var name = $(this).attr("alt");
$.ajax({
url: "deletepicsform.aspx",
data: { picname: name },
datatype: "json",
type: "post",
success: function(data, textstatus) {
alert(data.result);
},
error: function(xmlhttprequest, textstatus, errorthrown) {
alert(xmlhttprequest);
}
});
});
});
showpics.htm:
复制代码 代码如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>untitled page</title>
<script src="js/jquery-1.4.4.js" type="text/javascript"></script>
<script src="js/json2.js" type="text/javascript"></script>
<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>
</head>
<body>
<div>
<img src="images/xiyangyang.jpg" alt="xiyangyang.jpg" />
</div>
</body>
</html>
具体的删除的页面的代码如下:
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();
}
}
对于上面图片名称的传递,是用的get方式,想换成post方式可以用如下的方法:
复制代码 代码如下:
$(function() {
$("body img").click(function() {
var name = $(this).attr("alt");
$.ajax({
url: "deletepicsform.aspx",
data: { picname: name },
datatype: "json",
type: "post",
success: function(data, textstatus) {
alert(data.result);
},
error: function(xmlhttprequest, textstatus, errorthrown) {
alert(xmlhttprequest);
}
});
});
});
上一篇: Java实现SSH模式加密
下一篇: C#读取中文字符及清空缓冲区的实现代码