Asp.net 头像的简单实现
1:创建数据库
Create Table ImageSave ( ID int PRIMARY KEY IDENTITY(1,1), Name nvarchar(50), img image )
2:上传照片页关键代码
前台:
<div> <asp:FileUpload ID="fileUpImage" runat="server" /> <asp:Button ID="btnSubmit" runat="server" Text="提交" OnClick="btnSubmit_Click" /> <image src="Image.aspx" class="cs"></image> </div>
<style> .cs { border-radius: 25px; width: 50px; height: 50px; } </style>
后台:
protected void btnSubmit_Click(object sender, EventArgs e) { ImageTestDataContext itDataContext = new ImageTestDataContext(); Web.ImageSave img = new Web.ImageSave(); byte[] image = new byte[fileUpImage.PostedFile.ContentLength]; fileUpImage.PostedFile.InputStream.Read(image, 0, fileUpImage.PostedFile.ContentLength); img.Name = "测试" + new Random().Next(0,100); img.img = image; itDataContext.ImageSave.InsertOnSubmit(img); itDataContext.SubmitChanges(); }
3:上面代码中图片链接的代码
前台不用写,下面为后台代码:
protected void Page_Load(object sender, EventArgs e) { ImageTestDataContext itDataContext = new ImageTestDataContext(); Web.ImageSave img = itDataContext.ImageSave.OrderByDescending(p => p.ID).FirstOrDefault(); byte[] buffer = (byte[])img.img.ToArray(); Response.ContentType = buffer.ToString(); //System.Byte[] Response.BinaryWrite(buffer); }
4:效果