Home ASP.NET Hosting Dedicated Servers Contact Us
Announcements
Virtual Tiers
.NET Applications
XML Web Services
SQL Server Database
Web Traffic Statistics
Much More...
ASP.NET Web Site Hosting
Dedicated Servers
Windows 2003 Server
2.4 GHz Pentium 4
1024 MB RAM
80 GB Hard Drive
1000 GB/month
Fully Managed
Free Setup!
$268.00/month
Windows Dedicated Servers
Specialized Plans
10 or more Domains
Windows Services
Custom Plans
Windows Services
Learning & Support
About Us
ASP.NET Tutorial Web Sites
123aspx.com
411asp.net
dotnetfreaks.com
wwwcoder.com



ASP.NET Tutorial: Upload Image Files using ASP.NET and Visual Basic.NET



Learn More about Server Intellect



Download the Visual Studio.NET 2003 VB.NET Project Here

More ASP.NET Tutorials

Uploading Images using ASP.NET is surprisingly simple. Lets get started. The below code will allow you to Upload Image Files to your web site.

First we call the UploadImage() Method from the btnUpload.Click Event.

Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click

UploadImage()

End Sub

The UploadImage() Method

Notice that we use a Select Case statement to find out what type of image is being uploaded. This is very useful if you want to ensure you are uploading the correct file type, or want to upload certain image types to certain directories.

Sub UploadImage()

If Not (fileupload1.PostedFile Is Nothing) Then 'Check to make sure we actually have a file to upload

Dim strLongFilePath As String = fileupload1.PostedFile.FileName
Dim intFileNameLength As Integer = InStr(1, StrReverse(strLongFilePath), "\")
Dim strFileName As String = Mid(strLongFilePath, (Len(strLongFilePath) - intFileNameLength) + 2)

Select Case fileupload1.PostedFile.ContentType
Case "image/pjpeg", "image/jpeg" 'Make sure we are getting a valid JPG image
fileupload1.PostedFile.SaveAs(Server.MapPath("\") & strFileName)
lbStatus.Text = strFileName & " was uploaded successfully to: " & Server.MapPath("\") & strFileName
Case Else
'Not a valid jpeg image
lbStatus.Text = "Not a valid jpeg image"
End Select
End If

End Sub


The flow for the code behind page is as follows

Public Class index
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents btnUpload As System.Web.UI.WebControls.Button
Protected WithEvents fileupload1 As System.Web.UI.HtmlControls.HtmlInputFile
Protected WithEvents lbStatus As System.Web.UI.WebControls.Label

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub

Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
UploadImage()
End Sub

Sub UploadImage()

If Not (fileupload1.PostedFile Is Nothing) Then 'Check to make sure we actually have a file to upload

Dim strLongFilePath As String = fileupload1.PostedFile.FileName
Dim intFileNameLength As Integer = InStr(1, StrReverse(strLongFilePath), "\")
Dim strFileName As String = Mid(strLongFilePath, (Len(strLongFilePath) - intFileNameLength) + 2)

Select Case fileupload1.PostedFile.ContentType
Case "image/pjpeg", "image/jpeg" 'Make sure we are getting a valid JPG image
fileupload1.PostedFile.SaveAs(Server.MapPath("\") & strFileName)
lbStatus.Text = strFileName & " was uploaded successfully to: " & Server.MapPath("\") & strFileName
Case Else
'Not a valid jpeg image
lbStatus.Text = "Not a valid jpeg image"
End Select
End If

End Sub

End Class


Download the Visual Studio.NET 2003 VB.NET Project Here

Do you have an ASP.NET Tutorial you would like to see here? Contact Us!