有人說asp.net很簡單,其實呢這只是表面現象。要把asp.net學的很深真的是一件不容易的事情,從開發的角度來說asp.net要方便一些,只是移植性差了一些。學過java之後再來學習就會覺得很簡單,很快就可以上手。
下面就接着前面的博客寫一個權限控制和內置對象相結合的小例子,這個例子在學校裏可謂經典。所以我就借這個小例子來弄下。
1.開發一個用戶登錄表單,這裏只有登錄後的用戶才能進入下載頁面
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="downloadfile._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" action="ashx/ToLogin.ashx">
- 用戶名:<input type="text" name="fname"/><br /><br/>
- 密碼:<input type="password" name="password"/><br /><br/>
- <input type="submit" value="登錄"/>
- <input type="reset" value="取消"/>
- </form>
- </body>
- </html>
2.新建一個數據庫文件,並創建一個數據表
3.添加一個數據集,後面會使用類型化dataset來查詢數據庫
4.編寫一般處理程序處理用戶登錄
- using System;
- using System.Collections;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.Services;
- using System.Web.Services.Protocols;
- using System.Xml.Linq;
- using System.Web.SessionState;
- using downloadfile.DataSetTeacherTableAdapters;
- namespace downloadfile.ashx
- {
- /// <summary>
- /// $codebehindclassname$ 的摘要說明
- /// </summary>
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- public class ToLogin : IHttpHandler, IRequiresSessionState
- {
- public void Proce***equest(HttpContext context)
- {
- context.Response.ContentType = "text/plain";
- string fname = context.Request["fname"];
- string password = context.Request["password"];
- m_teacherTableAdapter adpater = new m_teacherTableAdapter();
- var data = adpater.GetDataByFname(fname);
- var single = data.Single();
- if (data.Count <= 0)
- {
- context.Response.Write("用戶名不存在");
- }
- else {
- //判斷數據的唯一性,防止出現未知錯誤
- //如果用戶輸入的密碼和數據庫的相同,則可以登錄成功
- if (single.fpassword.Equals(password))
- {
- context.Session["status"] = "login";
- context.Session["username"] = fname;
- context.Session["password"] = password;
- context.Session["userid"] = single.fid;
- //context.Response.Write("登錄成功");
- context.Response.Redirect("../manage/allpicture.htm");
- }else{
- context.Response.Redirect("../Default.aspx");
- }
- }
- }
- public bool IsReusable
- {
- get
- {
- return false;
- }
- }
- }
- }
5.編寫圖片的下載頁面
- <!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>
- <center>
- <div>
- <a href="../ashx/Download.ashx?filename=1.jpg">1.jpg</a>
- <a href="../ashx/Download.ashx?filename=2.jpg">2.jpg</a>
- <a href="../ashx/Download.ashx?filename=3.jpg">3.jpg</a>
- <a href="../ashx/Download.ashx?filename=4.jpg">4.jpg</a>
- </div>
- </center>
- </body>
- </html>
6.編寫下載的處理程序,如果是普通用戶則下載的圖片加水印
- using System;
- using System.Collections;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.Services;
- using System.Web.Services.Protocols;
- using System.Xml.Linq;
- using downloadfile.DataSetTeacherTableAdapters;
- using System.Web.SessionState;
- using System.Drawing;
- using System.Drawing.Imaging;
- namespace downloadfile.ashx
- {
- /// <summary>
- /// $codebehindclassname$ 的摘要說明
- /// </summary>
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- public class Download : IHttpHandler,IRequiresSessionState
- {
- public void Proce***equest(HttpContext context)
- {
- //如果用戶已經
- string status = (string)context.Session["status"];
- if (status!=null)
- {
- //取得文件名稱
- string filename=context.Request["filename"];
- //設置消息頭用來下載文件
- context.Response.ContentType="application/octet-stream";
- context.Response.AddHeader("Content-Disposition", string.Format("attachment;filename=\"{0}\"", filename));
- //從session取得登錄用戶的fid
- int fid=(int)context.Session["userid"];
- //根據用戶id查詢該用戶的級別,根據級別獲得不同的下載
- //數據集
- //通過用戶的id查詢用戶的級別
- m_teacherTableAdapter adpater = new m_teacherTableAdapter();
- var data = adpater.GetDataById(fid);
- var single = data.Single();
- if (single.level == 1)
- {
- //context.Response.WriteFile("../image/" + filename);
- using (Bitmap bitmap = new Bitmap(context.Server.MapPath("../image/" + filename)))
- {
- using (Graphics gra = Graphics.FromImage(bitmap))
- {
- gra.DrawString("免費下載", new Font("宋體", 20), Brushes.Red, 0, 0);
- }
- bitmap.Save(context.Response.OutputStream, ImageFormat.Jpeg);
- }
- }
- else {
- context.Response.WriteFile("../image/" + filename);
- }
- }
- else
- {
- context.Response.Write(status);
- //context.Response.Write("請先登錄");
- }
- }
- public bool IsReusable
- {
- get
- {
- return false;
- }
- }
- }
- }
7.程序的運行效果