943,929 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 15784
  • ASP.NET RSS
Jan 19th, 2009
0

Filtering the file extensions in Fileupload Control

Expand Post »
Hi,
I am having a FileUpload control in my application. When the application is running and if i click on browse button of fileupload control, i have to view only files which are having extension ".doc" (MS Word documents) to upload. How can i achieve this one. Pls help me out on this.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nmakkena is offline Offline
14 posts
since Dec 2008
Jan 19th, 2009
0

Re: Filtering the file extensions in Fileupload Control

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

ASP.NET Syntax (Toggle Plain Text)
  1.  
  2. function checkFileExtension(elem) {
  3. var filePath = elem.value;
  4.  
  5. if(filePath.indexOf('.') == -1)
  6. return false;
  7.  
  8. var validExtensions = new Array();
  9. var ext = filePath.substring(filePath.lastIndexOf('.') + 1).toLowerCase();
  10. //Add valid extentions in this array
  11. validExtensions[0] = 'doc';
  12. //validExtensions[1] = 'pdf';
  13.  
  14. for(var i = 0; i < validExtensions.length; i++) {
  15. if(ext == validExtensions[i])
  16. return true;
  17. }
  18.  
  19. alert('The file extension ' + ext.toUpperCase() + ' is not allowed!');
  20. return false;
  21. }

On your file upload Control

<asp:FileUpload ID="FileUpload1" runat="server" onchange ="checkFileExtension(this);" />
Reputation Points: 16
Solved Threads: 18
Junior Poster
Aneesh_Argent is offline Offline
104 posts
since Dec 2008
Jan 19th, 2009
0

Re: Filtering the file extensions in Fileupload Control

this article has both server and javascript solutions:
http://aspalliance.com/1614_Adding_F..._ASPNET_20.all
Reputation Points: 68
Solved Threads: 9
Junior Poster
sedgey is offline Offline
130 posts
since Jan 2007
Jan 20th, 2009
0

Re: Filtering the file extensions in Fileupload Control

Hi there

This peace of code can be handy to deal with file extensions.

ASP.NET Syntax (Toggle Plain Text)
  1. Public Shared Function GetFileName(ByVal Path As String) As String
  2. Try
  3. Dim RetVal As String
  4. Dim dt As String
  5. dt = Format(Date.Today, "yyMMdd")
  6. RetVal = Path & "\" & dt & ".xls"
  7. 'RetVal = Path & "\" & "test.BIL"
  8. If File.Exists(RetVal) = True Then
  9. Return RetVal
  10. Else
  11. RetVal = Nothing
  12. End If
  13. Catch ex As Exception
  14. MsgBox(ex.Message)
  15. End Try
  16. End Function
  17.  

Mark as solved, if it helps you

Regards,
Reputation Points: 12
Solved Threads: 38
Junior Poster
reach_yousuf is offline Offline
194 posts
since Sep 2007
Jul 19th, 2011
0
Re: Filtering the file extensions in Fileupload Control
Reputation Points: 10
Solved Threads: 1
Newbie Poster
tjaank is offline Offline
8 posts
since Jul 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: javascript alert Popups are not working in UPdate panel in ASP.net (Plz help me out))
Next Thread in ASP.NET Forum Timeline: How to get username from OpenId





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC