<scripttype="text/javascript">functionopenfile(){try{varfd=newActiveXObject("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>
<script type="text/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.net中的FileUpload不提供File Filter功能,而且也不能使用OpenFileDialog。那就只有通过JavaScript实现
调用:
<asp:TextBox ID="txtFilePath" runat="server" Width="300px" />
<input type="button" onclick="openfile()" value="Browse..." />