Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old October 28th, 2005, 09:03 AM
Registered User
Join Date: Oct 2005
Location: Coventry, Warwickshire, United Kingdom.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default FileUpload FIlter

Is it possible when using the FileUpload component, to specify the file extensions to be browsed and consequently uploaded ? ie ".jpg", ".gif" etc.

I am using the following code, which works, but browses all file types!:

Sub UploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        ' Before attempting to save the file, verify
        ' that the FileUpload control contains a file.
        If (FileUpload1.HasFile) Then
          ' Call a helper method routine to save the file.
          ' Notify the user that a file was not uploaded.
          UploadStatusLabel.Text = "You did not specify a file to upload."
        End If

      End Sub

      Sub SaveFile(ByVal file As HttpPostedFile)

        ' Specify the path to save the uploaded file to.
            Dim savePath As String = "\images\"

        ' Get the name of the file to upload.
        Dim fileName As String = FileUpload1.FileName

        ' Create the path and file name to check for duplicates.
        Dim pathToCheck As String = savePath + fileName

        ' Create a temporary file name to use for checking duplicates.
        Dim tempfileName As String

        ' Check to see if a file already exists with the
        ' same name as the file to upload.
        If (System.IO.File.Exists(pathToCheck)) Then
          Dim counter As Integer = 2
          While (System.IO.File.Exists(pathToCheck))
            ' If a file with this name already exists,
            ' prefix the filename with a number.
            tempfileName = counter.ToString() + fileName
            pathToCheck = savePath + tempfileName
            counter = counter + 1
          End While

          fileName = tempfileName

          ' Notify the user that the file name was changed.
          UploadStatusLabel.Text = "A file with the same name already exists." + "<br>" + _
                                   "Your file was saved as " + fileName
          ' Update Bound Data Field
          TxtImgUrl.Text = "images\" + fileName


          ' Notify the user that the file was saved successfully.
          UploadStatusLabel.Text = "Your file was uploaded successfully."

        End If

        ' Append the name of the file to upload to the path.
        savePath += fileName

        ' Call the SaveAs method to save the uploaded
        ' file to the specified directory.

      End Sub

  #2 (permalink)  
Old October 28th, 2005, 06:55 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts

Hi there,

AFAIK, there isn't a direct way to do this. However, right before you try to save the file you could use a Regular Expression to test the extension and /or filename.


Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Escape by Muse (Track 10 from the album: Showbiz) What's This?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Fileupload in Ajax bobwith5 ASP.NET 2.0 Professional 3 October 18th, 2008 09:59 AM
FileUpload "Rooted" error rsearing ASP.NET 2.0 Basics 4 July 20th, 2007 01:17 AM
FileUpload control aliirfan84 ASP.NET 2.0 Professional 1 June 2nd, 2007 03:37 PM
FileUpload Control MunishBhatia ASP.NET 2.0 Basics 2 December 6th, 2006 04:03 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.