C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ASP.NET WebForms »

Using FileUpload control and its validation in asp.net


Posted Date: 29 Oct 2008    Resource Type: Code Snippets    Category: ASP.NET WebForms
Author: Pradeep IyerMember Level: Diamond    
Rating: 1 out of 5Points: 10



This sample VB.NET code explains how to use the FileUpload control in asp.net web forms. The code implements a FileUpload control and a button to attach the uploaded files. The attachment works just the same as in gmail pages.


FileUpload.aspx
***************

( PROVIDED AS AN ATTACHMENT )

Here two types of validations are provided for the Fileupload control. One is RequiredField validation and another is RegularExpression validation. The former checks whether a file is selected for attachment and the later checks whether only txt, doc, pdf, jpg, jpeg, xls and png files are selected to attach.
These validations are very useful in developing asp.net forms.


FileUpload.aspx.vb
******************


Private _adapter As New InformationTableAdapters.InformationTableAdapter
Private _adapterActions As New ActionsTableAdapters.ActionsTableAdapter()
Private Const _maxAttachments As Integer = 10
Private _documentNames As String
Private _fileNames As String
Private _fileSize As String
Private _actionRequestId As Long = 0.0


Protected Sub btnAttach_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAttach.Click
If Me.IsValidNames() Then
Me.SaveAttachments()
End If
Me.DisplayAttachments()
End Sub


Private Sub SaveAttachments()
'Save file details into viewstate
Me._documentNames += "|" + Me.txtDocumentName.Text
Me._fileNames += "|" + Me.filUpload.FileName
Me._fileSize += "|" + Me.filUpload.PostedFile.ContentLength.ToString()

If (Me._actionRequestId = 0.0) Then Me._actionRequestId = Me.GetActionRequestId()

Me.SaveViewStateValues()

'Save uploaded file to server.
Me.filUpload.SaveAs(CommonFunctions.GetRequestFilePath(Me.SelectedVendor.Value, Me._actionRequestId, Me.filUpload.FileName))

'Clear document name after attaching.
Me.txtDocumentName.Text = ""
Me.rgxFileType.IsValid = True

'Enable Or Disable Attach button.
Me.DisableAttachButton()

End Sub


Private Sub DisplayAttachments()
If Me._documentNames Is Nothing OrElse Me._fileNames Is Nothing Then Exit Sub

Me._documentNames = Me._documentNames.TrimStart("|")
Me._fileNames = Me._fileNames.TrimStart("|")

Dim documentNames As String() = Me._documentNames.Split("|")
Dim fileNames As String() = Me._fileNames.Split("|")

For loopIndex As Integer = 0 To documentNames.Length - 1
Dim tclDocName As New TableCell
Dim tclFileName As New TableCell
Dim tableRow As New TableRow

tclDocName.Text = documentNames(loopIndex).ToString()
tclFileName.Text = fileNames(loopIndex).ToString()

tableRow.Cells.Add(tclDocName)
tableRow.Cells.Add(tclFileName)

Me.tblSupportingDocs.Rows.Add(tableRow)

Next
End Sub




Attachments

  • FileUpload.aspx (22085-29432-FileUpload_aspx.txt)



  • Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    Fileupload control validation  .  Fileupload control in asp.net  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Inserting brakes in a long text
    Previous Resource: Validating Textboxes in Asp.Net
    Return to Discussion Resource Index
    Post New Resource
    Category: ASP.NET WebForms


    Post resources and earn money!
     
    Related Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use