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
AttachmentsFileUpload.aspx (22085-29432-FileUpload_aspx.txt)
|
No responses found. Be the first to respond and make money from revenue sharing program.
|