uplogoimage.aspx前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="uplogoimage.aspx.cs" Inherits="Default4" %>
<!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>
<script language="javascript" type="text/javascript" src="ajax/jquery.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpimage" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传" />
<asp:Image ID="Image1" runat="server" ImageUrl="~/Fileupimage/b3.jpg" Visible="False" /></div>
</form>
</body>
</html>
uplogoimage.aspx后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 周昕 2009-6-8 上传头像
/// </summary>
protected void Button1_Click(object sender, EventArgs e)
{
//判断上传控件中是否有值
if (FileUpimage.HasFile == false)
{
//Response.Write("<script lanage='javascript'> alert('请指定上传的头像')</script>");
string clientScript = "var falseimage=parent.document.getElementById('falseimage');$(falseimage).html('请指定上传的头像');";
Page.ClientScript.RegisterStartupScript(this.GetType(), "falseimage", clientScript, true);
}
else
{
string fType = FileUpimage.PostedFile.ContentType;//获取图像的类型
if (fType == "image/bmp" || fType == "image/gif" || fType == "image/pjpeg" || fType == "image/jpeg" || fType == "image/x-png")
{
//获取文件信息
FileInfo file = new FileInfo(FileUpimage.PostedFile.FileName);
///随机数据
Guid guid = Guid.NewGuid();
string stamp = guid.ToString("N");
//生成随机数
Random aa=new Random();
string number=aa.Next(10000).ToString();
//原始图片保存路径
string path = "~/Fileupimage/" + stamp + ".gif";
//缩略图保存路径
string spath = "~/Fileupimage/" + number + ".gif";
try
{
//原始图片保存
FileUpimage.SaveAs(Server.MapPath(path));
//缩略图保存
MakeThumbImage(Server.MapPath(path), Server.MapPath(spath), 200, 100);
//给隐藏的图片控件赋值并显示
//Image1.Visible = true;
//Image1.ImageUrl = spath;
string clientScript = "var userInfo1=parent.document.getElementById('userInfo');$(userInfo1).attr('src','Fileupimage/" + number + ".gif" + "');$(userInfo1).show();";
clientScript+="var falseimage=parent.document.getElementById('falseimage');$(falseimage).html('上传成功');";
Page.ClientScript.RegisterStartupScript(this.GetType(), "successimage", clientScript, true);
//Response.Write("<script lanage='javascript'> alert('上传成功')</script>");
}
catch
{
//Response.Write("<script lanage='javascript'> alert('上传失败')</script>");
string clientScript = "var falseimage=parent.document.getElementById('falseimage');$(falseimage).html('上传失败');";
Page.ClientScript.RegisterStartupScript(this.GetType(), "falseimage", clientScript, true);
}
}
else
{
//Response.Write("<script lanage='javascript'> alert('上传头像格式不正确')</script>");
string clientScript = "var falseimage=parent.document.getElementById('falseimage');$(falseimage).html('上传头像格式不正确');";
Page.ClientScript.RegisterStartupScript(this.GetType(), "falseimage", clientScript, true);
}
}
}
///<summary>
///创建日期:2009-6-08
///创建人 :周昕
///方法名称:MakeThumbImage
///内容摘要:生成缩略图
/// </summary>
/// <param name="sPath">源图路径(物理路径)</param>
/// <param name="stPath">缩略图路径(物理路径)</param>
/// <param name="nWidth">缩略图宽度</param>
/// <param name="nHeight">缩略图高度</param>
private void MakeThumbImage(string sPath, string stPath, int nWidth, int nHeight)
{
System.Drawing.Image sImage = System.Drawing.Image.FromFile(sPath);
int tw = nWidth;
int th = nHeight;
///原始图片的宽度和高度
int sw = sImage.Width;
int sh = sImage.Height;
if (sw > tw)
{
sw = tw;
}
if (sh > th)
{
sh = th;
}
System.Drawing.Bitmap objPic, objNewPic;
objPic = new System.Drawing.Bitmap(sPath);
objNewPic = new System.Drawing.Bitmap(objPic, sw, sh);
objNewPic.Save(stPath);
sImage.Dispose();
objPic.Dispose();
objNewPic.Dispose();
}
}
upImage.aspx前台代码:调用uplogoimage.aspx页面进行上传。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upImage.aspx.cs" Inherits="Default3" %>
<!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>
<script language="javascript" type="text/javascript" src="ajax/jquery.js"></script>
<script language="javascript" type="text/javascript">
$("document").ready(function(){$("#userInfo").hide()});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td style="height: 260px">
<iframe id="uploadLogo" frameborder="0" scrolling="no" src="uplogoimage.aspx" style="width: 300px; height: 100px; margin-top:10px"></iframe></td>
<td style="height: 260px; width: 236px;"><img src="" id="userInfo" /><span id='falseimage'></span> </td></tr> </table>
</div>
</form>
</body>
</html>
upImage.aspx后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}