asp.net中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文件代码如下

<%@ WebHandler Language="C#" Class="ApiGetPhoto" %>

using System;
using System.Web;
using System.IO;
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 photoList = albumService.GetAllPhoto(int.Parse(userid));
        string counts = photoList.Count.ToString();
        //重点掌握json对象,注意引入Newtonsoft.Json.Linq和System.Collections.Generic
        //注意layui中table返回数据的json格式如下,如果是自己独有的json格式,需要在
        //table.render中添加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文件代码如下

<%@ WebHandler Language="C#" Class="ApiAddPhoto" %>

using System;
using System.Web;
using System.IO;
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文件代码如下

<%@ WebHandler Language="C#" Class="ApiDeletePhoto" %>

using System;
using System.Web;
using MyBlog.BLL;
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" %>





    
    
    


    

BLL层服务操作类AlbumService.cs代码如下,MyBlogDataContext 为linq to sql类,是由数据库文件直接转换而来

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyBlog.DAL;

namespace MyBlog.BLL
{
    
    public class AlbumService
    {
        MyBlogDataContext db = new MyBlogDataContext();
        //插入图片
        public void insertPhoto(int _userId,string _photoName,string _photoUrl,DateTime _dateTime)
        {
            photo photoItem = new photo
            {
                userId = _userId,
                photoName = _photoName,
                photoUrl = _photoUrl,
                photoTime=_dateTime
            };
            db.photo.InsertOnSubmit(photoItem);
            db.SubmitChanges();
        }

        public List GetAllPhoto(int _userId)
        {
            List photoList = new List();

            var x=from r in db.photo
                  where r.userId==_userId 
                  select r ;
            photoList = x.ToList();
            return photoList; 
        }

        public string delPhoto(int _photoId)
        {
            var x = from r in db.photo
                    where r.photoId == _photoId
                    select r;
            string pUrl=null;
            foreach (var item in x)
            {
                pUrl = item.photoUrl;
            }
            db.photo.DeleteAllOnSubmit(x);
            db.SubmitChanges();
            return pUrl;//返回被删除图片的url,从而在服务器上对该图片进行删除
        }

        public bool updatePhoto(int _photoId,string _photoName)
        {
            bool flag = false;
            var x = from r in db.photo
                    where r.photoId == _photoId
                    select r;
            if(x!=null)
            {
                foreach (var item in x)
                {
                    item.photoName = _photoName;
                    flag = true;
                }
            }
            db.SubmitChanges();
            return flag;
        }

    }

    
}

效果图如下
asp.net中ashx后台接口获取Layui前台ajax传送过来的参数_第1张图片

asp.net中ashx后台接口获取Layui前台ajax传送过来的参数_第2张图片

你可能感兴趣的:(Web)