Adding Filter Action to FileUpload Control of ASP.NET 2.0
page 4 of 6
by Soyuj Kumar Sahoo
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 12065/ 663

Server Side Approach

In the btnUpload_Click() event of "btnUpload" we have to add the following code, which will check the maximum size of the file and also filter the extension.

Listing 4: Server side filter action

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
 Handles btnUpload.Click
Try
Dim intDocFileLength As Integer = Me.fileDocument.PostedFile.ContentLength
Dim strPostedFileName As String = Me.fileDocument.PostedFile.FileName
 
If intDocFileLength > 4096000 Then '4MB
  Me.lblUploadErr.Text = "Image file size exceeds the limit of 4000 kb."
  Exit Sub
End If
 
If (strPostedFileName <> String.Empty) Then
  Dim strExtn As String = System.IO.Path.GetExtension(strPostedFileName).ToLower
 
  If (strExtn = ".txt"Or (strExtn = ".doc"Or (strExtn = ".rtf"Or _
     (strExtn = ".pdf"Or (strExtn = ".sxw"Or (strExtn = ".odt"Or _
     (strExtn = ".html"Or (strExtn = ".stw"Or (strExtn = ".htm"Or _
     (strExtn = ".sdw"Or (strExtn = ".vor"Then
 
    Dim savePath As String = _
      Server.MapPath(ConfigurationManager.AppSettings("UploadedPath")) & "\"
 
    Me.fileDocument.PostedFile.SaveAs(savePath & _
                                      System.IO.Path.GetFileName(strPostedFileName))
 
    Me.lblUploadErr.Text = "Document uploaded successfully..."
  Else
    Me.lblUploadErr.Text = _
      "Please upload a valid Document with the extenion in : <br>"
    Me.lblUploadErr.Text + = _
      ".txt, .doc, .rtf, .pdf, .sxw, .odt, .stw, .html, .htm, .sdw Or .vor"
  End If
Else
  Me.lblUploadErr.Text = "There is no file to Upload."
End If
Catch ex As Exception
'TODO: Add the Error log entry logic!
End Try
End Sub

Let us move through the codes. The fileDocument.PostedFile.ContentLength property of FileUpload control will give the size of the posted file as bytes and the fileDocument.PostedFile.FileName property will give the file name of the posted one. Then we check for the maximum size which is fixed to 4 MB as 4096000 bytes here. Then we get the file extension using the System.IO.Path.GetExtension() method of the input file. We match the posted extension with our given extensions. If the match satisfies, we do the Uploading action or else we show the error message in label.


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 5 and 4 and type the answer here:

User Comments

Title: How to retrieve File date info?   
Name: NP
Date: 8/14/2008 10:12:15 PM
Comment:
Is there a way to retrieve last modified or created dates for file using the file upload control? I use file upload control to mainly get the file bytes and use it to store file contents in database.
However, my requirement is also to have original file date from client side and to store it in database. I am unable to get the date stamp from client side
Appreciate any help.
Title: File Upload Issue   
Name: Rekha
Date: 7/28/2008 1:37:35 AM
Comment:
Hi..
When I click on the Browse button the Choose File dialog should open in thumbnail view always.. Is this possible and if yes then how?
Pls help
Title: Thanks   
Name: b@2te
Date: 7/22/2008 2:42:06 AM
Comment:
Thanks
Title: Good   
Name: Mr.6
Date: 7/1/2008 2:38:23 AM
Comment:
this article is very good.
Title: Good article   
Name: Mehul Bhuva
Date: 4/9/2008 6:53:20 AM
Comment:
It is a nice article..Keep it up
Title: Excellent Work   
Name: ali raza
Date: 4/8/2008 7:40:38 AM
Comment:
nice article, keep working
Title: File Filters   
Name: Haissam Abdul Malak
Date: 4/3/2008 2:00:41 AM
Comment:
Good article. However, you can use flash and ASP.NET to set the file filters on the open dialog box itself and to upload multiple files at one.
http://dotnetslackers.com/community/blogs/haissam/archive/2008/03/29/upload-multiple-files-using-asp-net-amp-adobe-flex.aspx

Product Spotlight
Product Spotlight 
Learn More
.NET Tools
asp.net shopping cart
asp.net chart control






Ads Powered by Lake Quincy Media
Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2008 ASPAlliance.com  |  Page Processed at 8/29/2008 5:23:01 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search