Attachments

Upload a File to the File System

Description
In some circumstances, it is useful for an application to upload a file to the file system instead of into the database. Uploading a file into the database is a straightforward matter of using the File Upload display style of the Field Value code generation tag. However, a small code customization is needed to upload a file to the file system instead of to the database.
Variables
Upload Button Control
Select the upload button
Applies to
P_Upload Button Control class
Code
 
''' 
''' Click handler for Button that uploads a file to a Data subfolder which exists 
''' in the current application directory.
'''     
Public Overrides Sub ${Upload Button Control}_Click(ByVal sender As Object, ByVal args As EventArgs)

    Dim inputFile As System.Web.UI.HtmlControls.HtmlInputFile
	
    inputFile = CType(Me.Page.FindControlRecursively("inputFile"), System.Web.UI.HtmlControls.HtmlInputFile)
    '' If this code customization was added to Table Control Row, replace FindControlRecursively() with FindControl()
	'' or comment out the above line of code and uncomment the line below
	'' inputFile = CType(Me.FindControl("inputFile"), System.Web.UI.HtmlControls.HtmlInputFile)
	   
    If (Not inputFile.PostedFile Is Nothing) And _
       (inputFile.PostedFile.ContentLength > 0) Then

        ' Get the name of the file to be uploaded and 
        ' the location where the file needs to be saved.
        Dim fileName As String = System.IO.Path.GetFileName(inputFile.PostedFile.FileName)
        Dim saveLocation As String = Me.Page.Server.MapPath("..\Data") & "\" & fileName
        Try
            'Save the file.
            inputFile.PostedFile.SaveAs(saveLocation)
            me.Page.Response.Write("The file has been uploaded.")
        Catch exc As Exception
            me.Page.Response.Write("Error: " & exc.Message)
        End Try
    Else
        me.Page.Response.Write("Please select a file to upload.")
    End If

End Sub
     

Terms of Service Privacy Statement