Asp.net FileUpload+Image制作头像效果示例代码(2)


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Fileup.aspx.cs" Inherits="Testcookie.Fileup" %>
<!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 runat="server">
<div>
<asp:Image runat="server" ImageUrl="~/Photo/untitled.png" />
<asp:FileUpload runat="server" />
<asp:Button runat="server" Text="Button"FONT-FAMILY: Arial, Helvetica, sans-serif">" /></SPAN>
<asp:Button runat="server" Text="Button" />
</div>
</form>
</body>
</html>


.axps.cs

复制代码 代码如下:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Testcookie
{
public partial class Fileup : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnupload_Click(object sender, EventArgs e)
{
int length= this.FileUpload1.PostedFile.ContentLength;//<SPAN>获取图片大小,以字节为单位</SPAN>
if(length<4000)
{
Response.Write("<script language='javascript'>alert('您选择的图片过大!');</script>");
}
else{
String type=this.FileUpload1.PostedFile.ContentType;//<SPAN>获取上传文件类型</SPAN>
String fullfilename = this.FileUpload1.PostedFile.FileName;//<SPAN>获取上传文件在客户端的路径及名称</SPAN>
String filename = fullfilename.Substring(fullfilename.LastIndexOf("\\")+1);
String extensions = filename.Substring(filename.LastIndexOf (".")+1);//<SPAN>获取文件扩展名</SPAN>
string name=DateTime.Now.ToString("yyyyMMddHHmmss");
if (type == "image/jpeg" || type == "image/png" )//限制上传格式
{
this.FileUpload1.SaveAs(Server.MapPath("Photo") + "\\" + name + "." + extensions);//<SPAN>将图片以当前时间命名保存,避免上传图片命名重复</SPAN>
<SPAN>//设置上传图片大小为原图片大小</SPAN>
//System.Drawing.Image img = System.Drawing.Image.FromFile(this.Server.MapPath("Photo") + "\\" + name + "." + extensions);//获得图片文件
//int width = img.Width;
//this.Image1.Width = width;//这是宽设置Image控件的宽度为图片宽度
//int height = img.Height;
//this.Image1.Height = height;//这是高,设置Image控件的高度为图片的高度
this.Image1.ImageUrl = "Photo/" + name + "." + extensions;
}
else
{
Response.Write("<script language='javascript'>alert('您选择的图片有误!');</script>");
}
}
}
protected void btndelete_Click (object sender, EventArgs e)
{
<SPAN>//删除图片</SPAN>
if (System.IO.File.Exists(Server.MapPath("Photo")+"\\20130808084433.PNG"))
{
try
{
System.IO.File.Delete(Server.MapPath("Photo") + "\\20130808084433.PNG");
}
catch
{
throw new Exception("文件删除失败!");
}
}
}
}
}


效果
文件上传前

Asp.net FileUpload+Image制作头像效果示例代码

 
文件上传后

Asp.net FileUpload+Image制作头像效果示例代码

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjgggs.html