1

使用一般处理程序上传文件

1.1上传
html 
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>上传文件</title>
    <meta charset="utf-8" />
</head>
<body>
    <form method="post" enctype="multipart/form-data" action="uploadHandler1.ashx">
        <input type="file" name="fileDemo" />
        <input type="submit" value="提交" />
    </form>
</body>
</html>

一般处理程序文件

namespace updown_load
{
    /// <summary>
    /// uploadHandler1 的摘要说明
    /// </summary>
    public class uploadHandler1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            HttpPostedFile file = context.Request.Files[0];
            file.SaveAs(context.Server.MapPath(Guid.NewGuid().ToString()+file.FileName));
            context.Response.Write("Hello World");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
View Code

使用一般处理文件下载文件

1.2下载
html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>下载页面</title>
    <meta charset="utf-8" />
</head>
<body>
    <a href="downloadHandler1.ashx">下载网页</a>
</body>
</html>

一般处理程序
namespace updown_load.File
{
    /// <summary>
    /// downloadHandler1 的摘要说明
    /// </summary>
    public class downloadHandler1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string encodeFileName = HttpUtility.UrlEncode("aaa.html");
            context.Response.AddHeader("content-Disposition", string.Format("attachment;filename=\"{0}\"", encodeFileName));
            context.Response.WriteFile("aaa.html");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
View Code

其中一般处理程序都实现了IHttpHandler接口

2 通过webfrom实现网络通讯的继承关系:

前台页面的aspx类——》asp.cs:codebehind(后置界面)——》page——》执行IhttpHandler:ProcessRequest

3 在aspx文件中会含有普通html标签,也会有服务器控件,他们执行的顺序从page顺序执行遇到普通html控件添加标签,继续执行,遇到服务器控件递归执行buildControltree;?(不晓得理解的正确吗)


4、IsPostBack介绍
Page.IsPostBack是一个标志:当前请求是否第一次打开。 调用方法为:Page.IsPostBack或者IsPostBack或者this.IsPostBack或者this.Page.IsPostBack,它们都等价。
  1)当通过IE的地址栏等方式打开一个URL时是第一次打开, 当通过页面的提交按钮或能引起提交的按钮以POST的方式提交的服务器时,页面就不再是第一次打开了。(每点击一次按钮,都是一次加载)
  2)IsPostBack只有在第一次打开的时候是false,其它时候都是true
  3).Net判断一个Page是否第一次打开的方法:Request.Form.Count>0
  4)每次页面Load的时候,根据需要把每次都要加载的代码放在IsPostBack中,只需要加载一次的代码放在if(!IsPostBack)中。
  5)每次用户回传服务器任何信息的时候,都会引发isPostBack属性用来判断此用户是否曾经做过登陆或者其他事件
  6 if(!IsPostBack)
  {
  Response.Write("第一次提交!");
  }
  if(IsPostBack)
  {
  Response.Write("按按钮!");
  }

 

 

5 模拟ispostback代码

html

  <form method="post" action="myIspostBackDemo.aspx">
        <input type="hidden" name="myIspostBack" value="1"/>
        <input type="submit" value="提交"/>
    </form>
View Code

cs

 public bool ispostbackDemo;
        //page-load函数是在一般处理程序中的ProcessRequest函数中执行的
        protected void Page_Load(object sender, EventArgs e)
        {
            var myIspostbackDemoShit = Request["myIspostBack"];
            ispostbackDemo = !string.IsNullOrEmpty(myIspostbackDemoShit);
            this.Response.Write(ispostbackDemo);
        }
View Code

 

posted on 2015-09-16 10:06  lvsally  阅读(206)  评论(0编辑  收藏  举报