Kevin

虫虫的痕迹!!!
  首页  :: 新随笔  :: 订阅 订阅  :: 管理

如何实现asp.net中FileUpload文件类型过滤功能

Posted on 2011-03-16 08:06  KevinYao  阅读(2804)  评论(0编辑  收藏  举报

Asp.net中的FileUpload不提供File Filter功能,而且也不能使用OpenFileDialog。那就只有通过JavaScript实现

<script language="javascript">
function openfile() {
try {
var fd = new ActiveXObject("MSComDlg.CommonDialog");
fd.Filter
= "上传文件 (*.jpg;*.jpeg;*.gif)|*.jpg;*.jpeg;*.gif";
fd.FilterIndex
= 2;
// 必须设置MaxFileSize. 否则出错
fd.MaxFileSize = 128;
fd.ShowOpen();
document.getElementById(
"txtFilePath").value = fd.Filename;
}
catch (e) {
document.getElementById(
"txtFileName").value = "";
}
}
</script>

调用:

 <asp:TextBox ID="txtFilePath" runat="server" Width="300px" />  

<input type="button" onclick="openfile()" value="Browse..." />

以上可以实现web中上传过滤。