<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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">
function UpLoad()
{
var ImgUrl=document.getElementById("FuLogo").value;
document.getElementById("ImgLogo").src=ImgUrl;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Image ID="ImgLogo" runat="server" Height="200px" Width="241px" /><br />
<asp:FileUpload ID="FuLogo" runat="server" Width="239px" />
<br />
<asp:Button ID="btnLogo" runat="server" OnClick="btnLogo_Click" Text="Up" /> </div>
</form>
</body>
</html>
//=======================================================
using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.FuLogo.Attributes.Add("onchange", "UpLoad()");
}
protected void btnLogo_Click(object sender, EventArgs e)
{
string Extension = Path.GetExtension(this.FuLogo.PostedFile.FileName.ToString());
if (Extension == ".jpg" || Extension==".gif")
{
if(File.Exists(Server.MapPath(@"Logo/" + FuLogo.FileName)))
{
Response.Write("<script>alert('The File is Exists,Please Chane FileName')</script>");
}
else
{
FuLogo.SaveAs(Server.MapPath(@"Logo/" + FuLogo.FileName.ToString()));
Response.Write("Up File Success");
}
}
else
{
Response.Write("<script>alert('Upload your file is incorrect')</script>");
}
}
}