《ASP.NET3.5网站开发项目实战 教学课件 ppt 作者 龚赤兵 任务5 显示图片》由会员分享,可在线阅读,更多相关《ASP.NET3.5网站开发项目实战 教学课件 ppt 作者 龚赤兵 任务5 显示图片(27页珍藏版)》请在金锄头文库上搜索。
1、02:43:19,1,任务5 显示图片,实训1新建数据库 实训2自定义HTTP处理程序,02:43:19,2,实训1新建数据库,新建PERSONAL数据库 分析PERSONAL数据库,02:43:19,3,新建PERSONAL数据库,02:43:19,4,新建PERSONAL数据库,02:43:19,5,新建PERSONAL数据库,02:43:19,6,新建PERSONAL数据库,02:43:19,7,分析PERSONAL数据库,02:43:19,8,实训2自定义HTTP处理程序,存放图片的目录结构 创建LINQ to SQL类 新建自定义HTTP处理程序,02:43:19,9,存放图片的目录
2、结构,02:43:19,10,创建LINQ to SQL类,1: public enum PhotoSize 2: 3: Small = 1, 4: Medium = 2, 5: Large = 3, 6: Original = 4 7: ,02:43:19,11,创建LINQ to SQL类,02:43:19,12,创建LINQ to SQL类,02:43:19,13,创建LINQ to SQL类,02:43:19,14,新建自定义HTTP处理程序,02:43:19,15,新建自定义HTTP处理程序,02:43:19,16,新建自定义HTTP处理程序,1: using System; 2:
3、using System.IO; 3: using System.Web; 4: using System.Linq; 5: using System.Data.Linq; 6: 7: public class Handler : IHttpHandler 8: 9: public bool IsReusable 10: 11: get 12: return true; 13: 14: 15: 16: public void ProcessRequest (HttpContext context) 17: 18: context.Response.ContentType = “image/jp
4、eg“; 19: context.Response.Cache.SetCacheability(HttpCacheability.Public); 20: context.Response.BufferOutput = false; 21: 22: PhotoSize size; 23:,02:43:19,17,新建自定义HTTP处理程序,24: switch (context.Request.QueryString“Size“) 25: 26: case “S“: 27: size = PhotoSize.Small; 28: break; 29: case “M“: 30: size =
5、PhotoSize.Medium; 31: break; 32: case “L“: 33: size = PhotoSize.Large; 34: break; 35: default: 36: size = PhotoSize.Original; 37: break; 38: 39: 40: Int32 id = -1; 41: Stream stream = null; 42: String path=null; 43: if (context.Request.QueryString“PhotoID“ != null 52: 53:,02:43:19,18,新建自定义HTTP处理程序,5
6、4: if (!path.Contains (“.jpg“) 55: path = GetPhoto(size); 56: 57: stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); 58: 59: const int buffersize = 1024 * 16; 60: byte buffer = new bytebuffersize; 61: int count = stream.Read(buffer, 0, buffersize); 62: while (count 0) 63:
7、 64: context.Response.OutputStream.Write(buffer, 0, count); 65: count = stream.Read(buffer, 0, buffersize); 66: 67: 68: 69: public static String GetPhoto(int photoId, PhotoSize size) 70: 71: PersonalDataContext dataContext = new PersonalDataContext(); 72: 73: var source = from p in dataContext.Photo
8、s where (p.PhotoID = photoId 76: 77:,02:43:19,19,新建自定义HTTP处理程序,78: public static String GetFirstPhoto(int albumId, PhotoSize size) 79: 80: PersonalDataContext dataContext = new PersonalDataContext(); 81: 82: var source = (from p in dataContext.Photos where (p.AlbumID = albumId 107: ,02:43:19,20,新建自定
9、义HTTP处理程序,108: 109: static private string GetPath(IQueryable qSource, PhotoSize size) 110: 111: string path = null; 112: 113: switch (size) 114: 115: case PhotoSize.Large: 116: path = (from p in qSource select p.LargeFileName).SingleOrDefault() ; 117: path = “Large/“+path ; 118: break; 119: case Pho
10、toSize.Original: 120: path = (from p in qSource select p.OriginalFileName).SingleOrDefault (); 121: break; 122: case PhotoSize.Small: 123: path = (from p in qSource select p.SmallFileName).SingleOrDefault (); 124: path = “Small/“ + path; 125: break; 126: default: 127: path = (from p in qSource selec
11、t p.MediumFileName).SingleOrDefault (); 128: path = “Medium/“ + path; 129: break; 130: ,02:43:19,21,新建自定义HTTP处理程序,131: 132: if (path !=null) 133: path= HttpContext.Current.Server.MapPath(“/Images/“)+path; 134: 135: return path; 136: 137: 138: public static bool IsFriend() 139: 140: return HttpContex
12、t.Current.User.IsInRole(“Friends“) | HttpContext.Current.User.IsInRole(“Administrators“); 141: 142: ,02:43:19,22,显示图片,1: 2: 3: Untitled Page 4: 5: 6: 7: 8: 9: 10: PhotoID 11: 12: 13: 14: 15: 16: Photo Size 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: ,02:43:19,23,显示图片,30: 31: 32: AlbumID 33:
13、34: 35: 36: 37: 38: Photo Size 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: ,02:43:19,24,显示图片,1: protected void Button1_Click(object sender, EventArgs e) 2: 3: int id = Convert.ToInt32(TextBox1.Text); 4: String size = TextBox2.Text; 5: 6: Image1.ImageUrl = “Handler.ashx?PhotoID=“ + id + “ 13: ,02:43:19,25,显示图片,02:43:19,26,显示图片,02:43:19,27,显示图片,