<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
/* Server对像
* Server是context的一个属性,是HttpServerUtility类的一个对像
* Server.HtmlDecode(), Server.HtmlEncode() Server.UrlEncode()
* Server.UrlDecode()是对HtmlUtility类中相应方法的一个代理调用,个人推荐总是使用HttpUtility,因为有的地方很难拿到Server对像
* 别把htmlEncode,urlEncode混了,UrlEncode是处理超链接,htmlEncode是处理html代码的
*
* Server.Transfer(path)内部重定向请求,Server.Transfer("JieBanRen.aspx")将用户的请求重定向给JieBanRen.aspx处理,是服务器内部的接管,浏览器是意识不到这个接管的,不像是Response.Redirect那样经历"通知浏览器说重新访问url这个网址"和浏览器接到命令访问新网址的过程
* 因此浏览器地址栏不会变化,因为内部接管,所以在预处理 定向到页面中是可以访问到Request,Cookies等这些来源页面接受的参数的,就像这些参数是传递给他的,
* 注意:Transfer是内部接管,因此不能像Redirect那样重定向到外部的网站
*
* 使用Server.Transfer不能直接重定向到ashx,否则会报错执行子请求出错
*
* 有的时候不能拿到HttpContext对像,比如在Global.aspx中(后面讲)可以能过HttpContext.Current拿到当前的HttpContext,进而合到Response, request,server等
*
* HttpHandler实现文件下载
* 如果HttpHandler输出的是html, txt, jpeg等类型的信息,那么浏览器会直显示,如果希望弹出保存对话框,则需要添加Header:
* string encodeFileName = HttpUtility.UrlEncode("过滤词.txt");
* Response.AddHeader("Content-Disposition",string.Format("attachment; filename=\"{0}\"", encodeFIleName));
* 其中filename后为编码后的文件名,filename段为建设的保存文件名
*
* 动态输出用处,不用再把资源保存到磁盘上再输出(不会有文件重名的问题,文件不生成在服务器端)
* 案例: 点周链接弹出图片下载支话框
*
* 练习: 用NPOI动态生成一个Execel表然后弹出对话框让用户下载,文件名是"用户列表.xls"
*
*
*/
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string q = Request["q"];
if (q == "1")
{
Response.Write("AA");
}
else if (q == "2")
{
//跳转到内页,内部接管
Server.Transfer("hello.aspx");
}
else if (string.IsNullOrEmpty(q))
{
Response.Write("请输入问题:");
}
else {
//这里是直接转换了页面,url有变化
Response.Redirect("hello.aspx?q="+q);
//Response.Redirect("3.jpg");
}
Response.Write("当前网站禁止访问!");
Response.End(); //当前停止访问
Response.Write("这里是看不到的!");
}
}
<%@ WebHandler Language="C#" Class="hello2" %>
using System;
using System.Web;
public class hello2 : IHttpHandler {
public void ProcessRequest (HttpContext context) {
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
context.Response.ContentType = "image/JPEG";
string name = context.Request["name"];
string filePath = HttpContext.Current.Server.MapPath("3.jpg");
using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(filePath))
{
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
{
g.DrawString(name, new System.Drawing.Font("宋体", 30), System.Drawing.Brushes.Red, 50, 50);
}
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
public bool IsReusable {
get {
return false;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class hello : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string q = Request["q"];
Response.Write(q);
}
}
<%@ WebHandler Language="C#" Class="xz" %>
using System;
using System.Web;
public class xz : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "image/JPEG";
//context.Response.Write("Hello World");
//好像浏览器不兼容
context.Response.AddHeader("Content-Disposition","attachment;filename=haha.jpg");
context.Response.WriteFile("3.jpg");
}
public bool IsReusable {
get {
return false;
}
}
}
<%@ WebHandler Language="C#" Class="访问者信息" %>
using System;
using System.Web;
public class 访问者信息 : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "image/JPEG";
//context.Response.Write("Hello World");
using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(300, 300))
{
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
{
g.DrawString("IP:" + context.Request.UserHostAddress, new System.Drawing.Font("宋体", 22), System.Drawing.Brushes.Red, 0, 0);
g.DrawString("操作系统:" + context.Request.Browser.Platform, new System.Drawing.Font("宋体", 22), System.Drawing.Brushes.Red, 0, 50);
g.DrawString("浏览器:" + context.Request.Browser.Type, new System.Drawing.Font("宋体", 22), System.Drawing.Brushes.Red, 0, 100);
}
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
public bool IsReusable {
get {
return false;
}
}
}
<!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></title>
</head>
<body>
<a href="1.jpg">下载</a>
<a href="xz.ashx">弹出窗下载</a>
<a href="3.jpg">下载</a>
</body>
</html>