| | |
Filtering the file extensions in Fileupload Control
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2008
Posts: 104
Reputation:
Solved Threads: 18
Unfortunately you cannot set a filter for the file browser dialog, but you could add a JavaScript function that would validate the extension of the selected file when a new file is selected. Thus you can prompt the user when he makes an invalid selection.
JS function
On your file upload Control
<asp:FileUpload ID="FileUpload1" runat="server" onchange ="checkFileExtension(this);" />
JS function
ASP.NET Syntax (Toggle Plain Text)
function checkFileExtension(elem) { var filePath = elem.value; if(filePath.indexOf('.') == -1) return false; var validExtensions = new Array(); var ext = filePath.substring(filePath.lastIndexOf('.') + 1).toLowerCase(); //Add valid extentions in this array validExtensions[0] = 'doc'; //validExtensions[1] = 'pdf'; for(var i = 0; i < validExtensions.length; i++) { if(ext == validExtensions[i]) return true; } alert('The file extension ' + ext.toUpperCase() + ' is not allowed!'); return false; }
On your file upload Control
<asp:FileUpload ID="FileUpload1" runat="server" onchange ="checkFileExtension(this);" />
this article has both server and javascript solutions:
http://aspalliance.com/1614_Adding_F..._ASPNET_20.all
http://aspalliance.com/1614_Adding_F..._ASPNET_20.all
•
•
Join Date: Sep 2007
Posts: 66
Reputation:
Solved Threads: 12
Hi there
This peace of code can be handy to deal with file extensions.
Mark as solved, if it helps you
Regards,
This peace of code can be handy to deal with file extensions.
ASP.NET Syntax (Toggle Plain Text)
Public Shared Function GetFileName(ByVal Path As String) As String Try Dim RetVal As String Dim dt As String dt = Format(Date.Today, "yyMMdd") RetVal = Path & "\" & dt & ".xls" 'RetVal = Path & "\" & "test.BIL" If File.Exists(RetVal) = True Then Return RetVal Else RetVal = Nothing End If Catch ex As Exception MsgBox(ex.Message) End Try End Function
Mark as solved, if it helps you
Regards,
![]() |
Other Threads in the ASP.NET Forum
- Previous Thread: how to delete the last line of the text file using c#
- Next Thread: SQLAdapter - reading a value from the querystring
| Thread Tools | Search this Thread |
.net activexcontrol advice ajax alltypeofvideos appliances asp asp.net bc30451 beginner bottomasp.net box browser button c# cac checkbox click commonfunctions control css dataaccesslayer database datagridview datagridviewcheckbox datalist deadlock deployment development dgv dialog dropdownlist dynamic dynamically edit embeddingactivexcontrol expose fileuploader fill findcontrol flash formatdecimal formview gridview gudi iframe iis javascript listbox login microsoft mono mouse mssql multistepregistration news novell numerical objects opera panelmasterpagebuttoncontrols radio redirect registration relationaldatabases reportemail rotatepage save schoolproject search security sessionvariables silverlight smartcard smoobjects software sql-server sqlserver2005 ssl suse textbox tracking treeview unauthorized validatedate validation vb.net video videos virtualdirectory vista visualstudio web webapplications webdevelopemnt webdevelopment webprogramming webservice xsl youareanotmemberofthedebuggerusers





