Monday 31 December 2012

File Upload in asp.net using VB.NET

File Upload in asp.net using VB.NET



Step 1)  Create a Webpage name it as FileUploadDemo.vb

Step 2)  Add FileUpload Control and Button control
<div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="btnUploads" runat="server" OnClick="btnUploads_Click" Text="Upload" />
    </div>

Step 3) Create a uploads folder in WebApplication

Step 4) If Website  is in IIS Directory(for ex:C:\inetpub\wwwroot\Test\WebApplication1) ,make sure all access permissions set   for
IIS_IUSRS(machinename\IIS_USRS) user i.e read and write permissions for uploads folder.


Step 5) If Website is file based, no need to worry about permissions

Step 6) Once it is done, Add Event Handler for btnUpload shown above

     Protected  Sub btnUploads_Click(ByVal sender As Object, ByVal e As EventArgs)
            Try
                Dim stream As System.IO.Stream =  FileUpload1.FileContent
                Dim reader As System.IO.StreamReader =  New System.IO.StreamReader(stream)
                Dim content As String =  reader.ReadToEnd()
                System.IO.File.AppendAllText(MapPath("uploads/" + FileUpload1.FileName), content)
            Catch ex As Exception
                Response.Write(ex.Message)
            End Try
End Sub

No comments:

Post a Comment