Uploading Files with ASP.NET ,VB.NET

Posted by in ASP.NET category on for Advance level | Views : 8108 red flag

In this article, I will explain how to uplaod files using ASP.NET ,VB.NET.It is a simplest way of uploading the files.

Using ASP.NET, you can upload the  files respectively. they are

First thing you have to add the file filed control from HTML Controls from the toolbox.

add runat=server control property to the control

on the Form tag add the parameter encType="multipart/form-data".  

The following Inlinecode explains the concepts as follows.

<%@ Page Language="VB"%>
<%@ Import Namespace="System.Threading" %>

  <title>Simplest way to uplaod files in ASP.NET</title>

<script runat="server">

Protected Sub Upload(ByVal sender As Object, ByVal e As System.EventArgs)
        if FileUpload1.FileName <> "" then

            FileUpload1.SaveAs(Server.MapPath("~/upload/") + FileUpload1.FileName)
            Dim p As New Photo(ddcollections.SelectedValue, txtName.Text,
FileUpload1.FileName, txtDesc.Text)
            If txtDesc.Text <> "" And txtName.Text <> "" Then
                'hand off the new photo to the data access class
                Dim result As Boolean = PhotoDB.InsertPhoto(p)
                If result = True Then
                    Label1.Text = "File upload completed! "
                    ddcollections.SelectedIndex = 0
                    txtDesc.Text = ""
                    txtName.Text = ""

                    Label1.Text = "File upload did not complete successfully"
                End If
            End If
        end if

    End Sub


<form enctype="multipart/form-data" runat="server">

Select the photo you would like to share:<br />
&nbsp;<asp:FileUpload ID="FileUpload1" runat="server" Width="317px" />
<br />
<br />
<asp:TextBox ID="txtName" runat="server" Width="217px"></asp:TextBox>
Name<br />
<br />
<br />
<asp:TextBox ID="txtDesc" runat="server" Height="85px" Width="228px" TextMode="MultiLine"></asp:TextBox>
Description<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Upload" />
<br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>&nbsp;<br />
<br />
<asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="../secure/admin.aspx">Back to Admin...</asp:LinkButton><br />


Enjoy coding ,



Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Majith Basha
Member Level:
Member Status: Member
Member Since: 7/18/2008 11:49:59 PM

Login to vote for this post.

Comments or Responses

Posted by: Poster on: 7/24/2008
Hey, I think code starting with following line were not needed.

Dim p As New Photo(ddcollections.SelectedValue, txtName.Text, FileUpload1.FileName, txtDesc.Text) .

Also you can combine scattered code block into one code block to looks better and more understandable.

Please provide any example in C# if you have.

Login to post response

Comment using Facebook(Author doesn't get notification)