如何在ashx页面里面使用Response对象
<%@ WebHandler Language="C#" Class="SavePhoto" %>using System;using System.Web;using System.IO;using System.Drawing;public class SavePhoto : IHttpHandler{ public void ProcessRequest(HttpContext context) { //System.Web.UI.Page page = new System.Web.UI.Page(); context.Response.ContentType = "text/plain"; if (context.Request.Form["PHeight"] != null && context.Request.Form["PWidth"] != null && context.Request.Form["strBMP"] != null) { try { int height = int.Parse(context.Request.Form["PHeight"].ToString()); int width = int.Parse(context.Request.Form["PWidth"].ToString()); string strBmp = context.Request.Form["strBMP"].ToString(); SaveBmp(BuildBitmap(width, height, strBmp), context.Server.MapPath("../../../images/CameraPhoto")); //System.Web.HttpContext.Current.Response.Write("<script>alert('111');</script>"); context.Response.Write("RetMsg=true"); } catch (Exception) { context.Response.Write("RetMsg=false"); } } } int height = int.Parse(context.Request.Form["PHeight"].ToString()); int width = int.Parse(context.Request.Form["PWidth"].ToString()); string strBmp = context.Request.Form["strBMP"].ToString(); SaveBmp(BuildBitmap(width, height, strBmp), context.Server.MapPath("../../../images/CameraPhoto")); //System.Web.HttpContext.Current.Response.Write("<script>alert('111');</script>"); context.Response.Write("<h2 style='color:green'>保存文件成功</h2><hr />文件已经保存到服务器路径:"+ context.Server.MapPath("../../../images/CameraPhoto"));
[解决办法]
handler中返回状态,比如:1:成功,2:失败
ajax的回调函数中。。
if(返回值 == "1")
{
alert("成功");
}
else
{
alert("失败");
}
[解决办法]
举个栗子.如下
/// <summary> /// 查询登陆 /// </summary> /// <param name="context"></param> public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string username = context.Request["username"]; string userpwd = context.Request["userpwd"]; string pwd = getsha256(userpwd); NRBLL.User bu = new Asiastar.NRBLL.User(); if (bu.FN_Exists(username, pwd)) { context.Session["username"] = username; context.Response.Write("0"); } else { context.Response.Write("1"); } }
[解决办法]
ashx 当然不能这么处理了
在调用页面,接受返回值后 用js 判断处理就可以了
[解决办法]