Filtering the file extensions in Fileupload Control

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 13
Reputation: nmakkena is an unknown quantity at this point 
Solved Threads: 0
nmakkena nmakkena is offline Offline
Newbie Poster

Filtering the file extensions in Fileupload Control

 
0
  #1
Jan 19th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 104
Reputation: Aneesh_Argent is an unknown quantity at this point 
Solved Threads: 18
Aneesh_Argent Aneesh_Argent is offline Offline
Junior Poster

Re: Filtering the file extensions in Fileupload Control

 
0
  #2
Jan 19th, 2009
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

  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);" />
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 130
Reputation: sedgey is on a distinguished road 
Solved Threads: 8
sedgey's Avatar
sedgey sedgey is offline Offline
Junior Poster

Re: Filtering the file extensions in Fileupload Control

 
0
  #3
Jan 19th, 2009
this article has both server and javascript solutions:
http://aspalliance.com/1614_Adding_F..._ASPNET_20.all
David Ridgway: so little daylight, too much caffeine
MCSD MCAD MCSE
http://web2asp.net
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 66
Reputation: reach_yousuf is an unknown quantity at this point 
Solved Threads: 12
reach_yousuf reach_yousuf is offline Offline
Junior Poster in Training

Re: Filtering the file extensions in Fileupload Control

 
0
  #4
Jan 20th, 2009
Hi there

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

  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,
Yousuf
Software Developer
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC