码迷,mamicode.com
首页 > Web开发 > 详细

Asp.net 头像的简单实现

时间:2015-07-02 11:28:36      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

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:效果

技术分享

    

Asp.net 头像的简单实现

标签:

原文地址:http://www.cnblogs.com/lipeng0824/p/4615224.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!