asp中ashx后台接⼝获取Layui前台ajax传送过来的参数后台代码:
//可以获取任何⽅式
string photoId = context.Request.Params["photoid"];
//POST⽅式传参
string photoId = context.Request.Form["photoid"];
//GET⽅式传参
string photoId = context.Request.QueryString["photoid"];
完整后台代码
ApiEditPhoto .ashx⽂件代码如下
<%@ WebHandler Language="C#" Class="ApiEditPhoto" %>
using System;
using System.Web;
using MyBlog.BLL;
using Newtonsoft.Json.Linq;
using System.IO;
public class ApiEditPhoto : IHttpHandler
{
AlbumService albumService = new AlbumService();
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
string photoId = context.Request.Params["photoid"];
string photoName = context.Request.Params["photoname"];
bool flag=albumService.updatePhoto(int.Parse(photoId),photoName);
JObject jo;
if(flag==true)
{
jo = new JObject(new JProperty("msg", "修改成功"));
}
else
{
jo = new JObject(new JProperty("msg", "修改失败"));
}
context.Response.Write(jo);
}
public bool IsReusable
{
get
{
return false;
}
}
}
ApiGetPhoto.ashx⽂件代码如下
using System.Data;
using System.Text;
using Newtonsoft.Json;
using MyBlog.BLL;
using MyBlog.DAL;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
//获得所有photo表的信息
public class ApiGetPhoto : IHttpHandler
{
AlbumService albumService = new AlbumService();
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
string userid = "2";
List<photo> photoList = albumService.GetAllPhoto(int.Parse(userid));
string counts = photoList.Count.ToString();
//重点掌握json对象,注意引⼊Newtonsoft.Json.Linq和System.Collections.Generic        //注意layui中table返回数据的json格式如下,如果是⾃⼰独有的json格式,需要在        //der中添加parseData字段,详见官⽅⽂档
JObject res = new JObject();
res.Add(new JProperty("code", 0));
res.Add(new JProperty("msg", ""));
res.Add(new JProperty("count", counts));
JArray photos = new JArray();
foreach (var item in photoList)
{
JObject p = new JObject();
p.Add(new JProperty("photoId", item.photoId));
p.Add(new JProperty("photoName", item.photoName));
p.Add(new JProperty("photoUrl", item.photoUrl));
p.Add(new JProperty("photoTime", item.photoTime));
photos.Add(p);
}
res.Add(new JProperty("data", photos));
context.Response.Write(res);
}
public bool IsReusable
{
get
{
return false;
}
}
}
ApiAddPhoto.ashx⽂件代码如下
using System.Data;
using System.Text;
using Newtonsoft.Json;
using MyBlog.BLL;
using Newtonsoft.Json.Linq;
public class ApiAddPhoto : IHttpHandler
{
AlbumService albumService=new AlbumService();
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
string savePath = HttpContext.Current.Server.MapPath("../UploadFiles/");        if (!System.IO.Directory.Exists(savePath))
{
System.IO.Directory.CreateDirectory(savePath);
}
HttpFileCollection hfc = context.Request.Files;
if (hfc.Count > 0)
{
HttpPostedFile hpf = context.Request.Files[0];
if (hpf.ContentLength > 0)
{
string fileName = Path.GetFileName(hpf.FileName);
savePath = savePath + "\\" + fileName;
hpf.SaveAs(savePath);
string path = string.Format("UploadFiles/{0}", fileName);
//法⼀
//string json = "{\"fileName\":\"" + path + "\"}";
//JObject jo = (JObject)JsonConvert.DeserializeObject(json);
//法⼆
JObject jObject=new JObject(new JProperty("fileName",path)) ;
context.Response.Write(jObject);
//获取当前时间
DateTime dateTime = DateTime.Now.ToLocalTime();
//插⼊数据库
albumService.insertPhoto(2, fileName, path, dateTime);
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
ApiDeletePhoto.ashx⽂件代码如下
using Newtonsoft.Json.Linq;
using System.IO;
public class ApiDeletePhoto : IHttpHandler
{
AlbumService albumService = new AlbumService();
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
string photoId = context.Request.Form["photoid"];
string url = albumService.delPhoto(int.Parse(photoId));
JObject jo;
if (url != null)
{//成功删除图⽚
string savePath = HttpContext.Current.Server.MapPath("../" + url);
File.Delete(savePath);
jo = new JObject(new JProperty("msg", "删除成功"));
}
else
{
jo = new JObject(new JProperty("msg", "删除失败"));
}
context.Response.Write(jo);
}
public bool IsReusable
{
get
{
return false;
}
}
}
前台完整代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyAlbum.aspx.cs" Inherits="MyAlbum" %> <!DOCTYPE html>
<html xmlns="/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="Contents/css/layui.css" rel="stylesheet" />
</head>
<body>
<table class="layui-hide" id="photoTable" lay-filter="test"></table>
<script type="text/html" id="toolbarDemo">
<div class="layui-btn-container">
<button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中⾏数据</button>
<button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数⽬</button>
<button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
</div>
</script>
<script type="text/html" id="barDemo">
<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script src="Contents/layui.js"></script>
<script>
layui.use(['table', 'jquery'], function () {
var table = layui.table;
var $ = layui.jquery;
elem: '#photoTable'
asp查看源码配置ui, url: '/Comm/ApiGetPhoto.ashx'
, toolbar: '#toolbarDemo' //开启头部⼯具栏,并为其绑定左侧模板,指向⾃定义⼯具栏模板选择器
, defaultToolbar: ['filter', 'exports', 'print', { //⾃定义头部⼯具栏右侧图标。如⽆需⾃定义,去除该参数即可                    title: '提⽰'
, layEvent: 'LAYTABLE_TIPS'
, icon: 'layui-icon-tips'
}]
, title: '⽤户数据表'
, cols: [[
{ type: 'checkbox', fixed: 'left' }
, { field: 'photoId', title: '照⽚ID', width: 120, fixed: 'left', unresize: true, sort: true }
, { field: 'photoName', title: '照⽚名', width: 120 }
, {
field: 'photoUrl', title: '照⽚地址', width: 200, templet: function (res) {
return '<em>' + res.photoUrl + '</em>'
}
}
, { field: 'photoTime', title: '上传时间', width: 180 }
, { fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150 }
]]
, page: true
});
//头⼯具栏事件
<('toolbar(test)', function (obj) {
var checkStatus = table.fig.id);
switch (obj.event) {
case 'getCheckData':
var data = checkStatus.data;
layer.alert(JSON.stringify(data));
break;
case 'getCheckLength':
var data = checkStatus.data;
layer.msg('选中了:' + data.length + ' 个');
break;
case 'isAll':
layer.msg(checkStatus.isAll ? '全选' : '未全选');
break;
/
/⾃定义头⼯具栏右侧图标 - 提⽰
case 'LAYTABLE_TIPS':
layer.alert('此表格是⽤于照⽚管理');
break;
};
});
//监听⾏⼯具事件
<('tool(test)', function (obj) {
var data = obj.data;
//console.log(obj)
if (obj.event === 'del') {
$.ajax({
type: "post",

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。