FileUpload Control

The FileUpload Control allow you to upload files to the server
- Open Visual Studio
- Add a new webform
- Name it fileUpload.aspx
- Add a label control and name is message
- Add a button control
- Double click the button and add this code:
If Page.IsValid Then
Try
FileUpload1.SaveAs( String.Concat( "C:\", FileUpload1.FileName))
message.Text = String.Concat( "The file '", FileUpload1.FileName, "' was successfully uploaded")
Catch ex As Exception
message.Text = String.Concat( "An error occurred uploading the file ", FileUpload1.FileName, "' – ", ex.Message)
End Try
End If
- Add a customValidator Control
- Double-click the validator control and add this code:
'Verify the control has a file
If Not FileUpload1.HasFile Then
CustomValidator1.ErrorMessage = "A file is required in order to proceed"
args.IsValid = False
Else
Dim ext As String = System.Web.VirtualPathUtility.GetExtension(FileUpload1.FileName).ToUpper()
If Not ext = ".GIF" And Not ext = ".JPG" Then
CustomValidator1.ErrorMessage = String.Concat( "Invalid file type '", etx, "' - must be .gif or .jpg to continue")
args.IsValid = False
Else
args.IsValid = True
End If
End If
