FileUpload上传文件时可以在客户端作一些简单验证,比如文件的扩展名等信息,也可以给出一些提示!下面是一个验证上传Excel文件的例子,由于Excel文件上传后,数据将导入到数据库中,所以在最后还加了一个确认提示!
1、页面代码:
<%
...
@ Page Language="C#" AutoEventWireup="true" CodeFile="Upload.aspx.cs" Inherits="Upload"
%>
<! 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 > Asp.net文件上传时的客户端简单验证 </ title >
< script language ="javascript" > ...
function checkmes(id)
...{
var filename = document.getElementById(id).value;
//验证是否选择文件
if(filename=="")
...{
alert("请选择一个文件!");
return false;
}
//验证扩展名
var ex = filename.substring(filename.length-4);
if(ex!=".xls")
...{
alert("你选择的文件类型不正确,请选择一个Excel文件!");
return false;
}
//最后提示信息
return confirm("你确定要上传此Excel文件?");
}
</ script >
</ head >
< body >
< form id ="form1" runat ="server" >
< div >
< strong > Asp.net文件上传时的客户端简单验证 < br />
< br />
< asp:FileUpload ID ="fu_Excel" runat ="server" Width ="300px" />
< asp:Button ID ="btn_Upload" runat ="server" Text ="上 传" OnClick ="btn_Upload_Click" /> </ strong ></ div >
</ form >
</ body >
</ html >
<! 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 > Asp.net文件上传时的客户端简单验证 </ title >
< script language ="javascript" > ...
function checkmes(id)
...{
var filename = document.getElementById(id).value;
//验证是否选择文件
if(filename=="")
...{
alert("请选择一个文件!");
return false;
}
//验证扩展名
var ex = filename.substring(filename.length-4);
if(ex!=".xls")
...{
alert("你选择的文件类型不正确,请选择一个Excel文件!");
return false;
}
//最后提示信息
return confirm("你确定要上传此Excel文件?");
}
</ script >
</ head >
< body >
< form id ="form1" runat ="server" >
< div >
< strong > Asp.net文件上传时的客户端简单验证 < br />
< br />
< asp:FileUpload ID ="fu_Excel" runat ="server" Width ="300px" />
< asp:Button ID ="btn_Upload" runat ="server" Text ="上 传" OnClick ="btn_Upload_Click" /> </ strong ></ div >
</ form >
</ body >
</ html >
2、后台代码:
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 Upload : System.Web.UI.Page
... {
protected void Page_Load(object sender, EventArgs e)
...{
btn_Upload.Attributes.Add("onclick", "javascript:return checkmes('" + fu_Excel.ClientID + "');");
}
protected void btn_Upload_Click(object sender, EventArgs e)
...{
//上传文件的服务器端代码
}
}
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 Upload : System.Web.UI.Page
... {
protected void Page_Load(object sender, EventArgs e)
...{
btn_Upload.Attributes.Add("onclick", "javascript:return checkmes('" + fu_Excel.ClientID + "');");
}
protected void btn_Upload_Click(object sender, EventArgs e)
...{
//上传文件的服务器端代码
}
}
以上只是提供一种实现的方式,具体的验证可根据需要作相应的修改。