其实很简单,主要是我没怎么好好看过js,所以还是想了一会儿才想出来。。。感觉这代码有种骗人的感觉


<%@ 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>Upload</title>
<script type="text/javascript" language="javascript">
function jsUpload(id,attributeName,attributeValue)
{
if(arguments[0] == null ||
arguments[1] == null ||
arguments[2] == null)
return true;
var v = document.getElementById(id);
v.setAttribute(attributeName,attributeValue);
return true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Label ID="Label1" runat="server" Text="Select a file to upload"></asp:Label>
<br />
<asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" />
</div>
</form>
</body>
</html>


using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
btnUpload.Attributes.Add("onclick",
"jsUpload('"+Label1.ClientID +"','innerText','Uploading

}
}
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
try
{
FileUpload1.SaveAs(Server.MapPath("~/") +
System.IO.Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName) +
Guid.NewGuid().ToString() +
System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName));
ClientScriptManager cs = this.ClientScript;
cs.RegisterClientScriptBlock(this.Page.GetType(), "Upload",
"alert('Successful!');", true);
}
catch
{
ClientScriptManager cs = this.ClientScript;
cs.RegisterClientScriptBlock(this.Page.GetType(), "NotUpload",
"alert('Failed!');", true);
}
}
}
}