Introduction:
This article describes three different approaches to uploading a file to a web server. The approaches are:
- Upload files directly to the server
- Upload files through a web service
- Upload files asynchronously through a web service
This article and included example code describe and illustrate each of these approaches. The examples will illustrate an approach to classifying files upon upload and directing the file to specific folders based upon the type of file uploaded.

Figure 1: File Upload Example Website
Getting Started:
In order to get started, unzip the included source code. Within the zip file you will note that there are two separate projects, one is the web site itself and the other one is a web service used to upload files. Create virtual directories for the website and web service and then add both to a solution within the Visual Studio 2005 IDE.
Both projects should be contained within a single solution in order to make it easier to read and manipulate the projects simultaneously.
The Code: Web Service.
The purpose of the web service is to manage file uploads to a collection of folders located in the server's path. The declarations and imports are all standard to the default template except for the import used to pull in the System.IO library; System.IO is used to capture the file extension from inbound file upload.
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.IO
' F I L E S T O R A G E W E B S E R V I C E
'
' This service contains two web methods:
' 1. SaveFile - Saves file to a folder with no file extension
' 2. SaveFileAsType - Saves file to folder with extension
'
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class FileStorage
Inherits System.Web.Services.WebService
The class, "FileStorage", exposes two web methods: "SaveFile" and "SaveFileAsType". "SaveFile" is used to save an inbound file without an extension into a specific folder while "SaveFileAsType" is used to save the inbound file with its extension intact and into specific folders used to hold different types of files (e.g., images or text). The code for the "SaveFile" web method is as follows:
<WebMethod()> _
Public Function SaveFile(ByVal sFileName As String, _
ByVal bytFileByteArr As Byte()) As Integer
' Saves a file with no file extension
' Get the file name and set a new path
' to the local storage folder
Dim strFile As String = _
System.IO.Path.GetFileNameWithoutExtension(sFileName)
strFile = System.Web.Hosting.HostingEnvironment.MapPath _
("~/StorageStuff/" & strFile)
'write the file out to the storage location
Try
Dim stream As New FileStream(strFile, FileMode.CreateNew)
stream.Write(bytFileByteArr, 0, bytFileByteArr.Length)
stream.Close()
Return 0
Catch ex As Exception
Return 1
End Try
End Function
In examining the code you will see that the method accepts a string used to contain the file name and a byte array which carries the content of the file itself. The method strips the file extension from the file name and defines a new path for the file; you will see that the file with an anonymous file type is passed to a trash can folder called "StorageStuff". Once the new path is defined and the file extension stripped from the file name, a file stream is created and the entire byte array containing the file is then passed to the stream. Upon instancing the file stream, the path defined previously is used to define the file name and storage location on the server. Once the byte array is written to the file location, the stream is closed and the method returns a 0 indicating success. If a failure occurs, the exception will be caught and the method will return a one indicating that the upload failed.
The second web method defined is entitled "SaveFileAsType"; this method is used to determine the file type passed into the method and to place the file into specific folders based upon that file type. The code for that web method is as follows:
<WebMethod()> _
Public Function SaveFileAsType(ByVal sFileName As String, _
ByVal bytFileByteArr As Byte()) As Integer
' Saves a file as a specific type with an extension
' Capture the file type through its extension
Dim sFileType As String
sFileType = System.IO.Path.GetExtension(sFileName)
sFileType = sFileType.ToLower()
' filter file options based upon the file type
Select Case sFileType
Case ".doc"
' Get the file name and set a new
' path to the local storage folder
Dim strFile As String = _
System.IO.Path.GetFileNameWithoutExtension(sFileName)
strFile = System.Web.Hosting.HostingEnvironment.MapPath _
("~/StoredText/" & strFile & sFileType)
'write the file out to the storage location
Try
Dim stream As New FileStream(strFile,
FileMode.OpenOrCreate)
stream.Write(bytFileByteArr, 0, bytFileByteArr.Length)
stream.Close()
Return 0
Catch ex As Exception
Return 1
End Try
Case ".docx"
' Get the file name and set a new
' path to the local storage folder
Dim strFile As String = _
System.IO.Path.GetFileNameWithoutExtension(sFileName)
strFile = System.Web.Hosting.HostingEnvironment.MapPath _
("~/StoredText/" & strFile & sFileType)
'write the file out to the storage location
Try
Dim stream As New FileStream(strFile,
FileMode.OpenOrCreate)
stream.Write(bytFileByteArr, 0, bytFileByteArr.Length)
stream.Close()
Return 0
Catch ex As Exception
Return 1
End Try
Case ".xml"
' Get the file name and set a new
' path to the local storage folder
Dim strFile As String = _
System.IO.Path.GetFileNameWithoutExtension(sFileName)
strFile = System.Web.Hosting.HostingEnvironment.MapPath _
("~/StoredText/" & strFile & sFileType)
'write the file out to the storage location
Try
Dim stream As New FileStream(strFile,
FileMode.OpenOrCreate)
stream.Write(bytFileByteArr, 0, bytFileByteArr.Length)
stream.Close()
Return 0
Catch ex As Exception
Return 1
End Try
Case ".rtf"
' Get the file name and set a new
' path to the local storage folder
Dim strFile As String = _
System.IO.Path.GetFileNameWithoutExtension(sFileName)
strFile = System.Web.Hosting.HostingEnvironment.MapPath _
("~/StoredText/" & strFile & sFileType)
'write the file out to the storage location
Try
Dim stream As New FileStream(strFile,
FileMode.OpenOrCreate)
stream.Write(bytFileByteArr, 0, bytFileByteArr.Length)
stream.Close()
Return 0
Catch ex As Exception
Return 1
End Try
Case ".lic"
' Get the file name and set a new
' path to the local storage folder
Dim strFile As String = _
System.IO.Path.GetFileNameWithoutExtension(sFileName)
strFile = System.Web.Hosting.HostingEnvironment.MapPath _
("~/StoredText/" & strFile & sFileType)
'write the file out to the storage location
Try
Dim stream As New FileStream(strFile,
FileMode.OpenOrCreate)
stream.Write(bytFileByteArr, 0, bytFileByteArr.Length)
stream.Close()
Return 0
Catch ex As Exception
Return 1
End Try
Case ".txt"
' Get the file name and set a new
' path to the local storage folder
Dim strFile As String = _
System.IO.Path.GetFileNameWithoutExtension(sFileName)
strFile = System.Web.Hosting.HostingEnvironment.MapPath _
("~/StoredText/" & strFile & sFileType)
'write the file out to the storage location
Try
Dim stream As New FileStream(strFile,
FileMode.OpenOrCreate)
stream.Write(bytFileByteArr, 0, bytFileByteArr.Length)
stream.Close()
Return 0
Catch ex As Exception
Return 1
End Try
Case ".html"
' Get the file name and set a new
' path to the local storage folder
Dim strFile As String = _
System.IO.Path.GetFileNameWithoutExtension(sFileName)
strFile = System.Web.Hosting.HostingEnvironment.MapPath _
("~/StoredText/" & strFile & sFileType)
'write the file out to the storage location
Try
Dim stream As New FileStream(strFile,
FileMode.OpenOrCreate)
stream.Write(bytFileByteArr, 0, bytFileByteArr.Length)
stream.Close()
Return 0
Catch ex As Exception
Return 1
End Try
Case ".htm"
' Get the file name and set a new
' path to the local storage folder
Dim strFile As String = _
System.IO.Path.GetFileNameWithoutExtension(sFileName)
strFile = System.Web.Hosting.HostingEnvironment.MapPath _
("~/StoredText/" & strFile & sFileType)
'write the file out to the storage location
Try
Dim stream As New FileStream(strFile,
FileMode.OpenOrCreate)
stream.Write(bytFileByteArr, 0, bytFileByteArr.Length)
stream.Close()
Return 0
Catch ex As Exception
Return 1
End Try
Case ".bmp"
' Get the file name and set a new
' path to the local storage folder
Dim strFile As String = _
System.IO.Path.GetFileNameWithoutExtension(sFileName)
strFile = System.Web.Hosting.HostingEnvironment.MapPath _
("~/StoredImages/" & strFile & sFileType)
'write the file out to the storage location
Try
Dim stream As New FileStream(strFile,
FileMode.OpenOrCreate)
stream.Write(bytFileByteArr, 0, bytFileByteArr.Length)
stream.Close()
Return 0
Catch ex As Exception
Return 1
End Try
Case ".jpg"
' Get the file name and set a new
' path to the local storage folder
Dim strFile As String = _
System.IO.Path.GetFileNameWithoutExtension(sFileName)
strFile = System.Web.Hosting.HostingEnvironment.MapPath _
("~/StoredImages/" & strFile & sFileType)
'write the file out to the storage location
Try
Dim stream As New FileStream(strFile,
FileMode.OpenOrCreate)
stream.Write(bytFileByteArr, 0, bytFileByteArr.Length)
stream.Close()
Return 0
Catch ex As Exception
Return 1
End Try
Case ".gif"
' Get the file name and set a new
' path to the local storage folder
Dim strFile As String = _
System.IO.Path.GetFileNameWithoutExtension(sFileName)
strFile = System.Web.Hosting.HostingEnvironment.MapPath _
("~/StoredImages/" & strFile & sFileType)
'write the file out to the storage location
Try
Dim stream As New FileStream(strFile,
FileMode.OpenOrCreate)
stream.Write(bytFileByteArr, 0, bytFileByteArr.Length)
stream.Close()
Return 0
Catch ex As Exception
Return 1
End Try
Case ".png"
' Get the file name and set a new
' path to the local storage folder
Dim strFile As String = _
System.IO.Path.GetFileNameWithoutExtension(sFileName)
strFile = System.Web.Hosting.HostingEnvironment.MapPath _
("~/StoredImages/" & strFile & sFileType)
'write the file out to the storage location
Try
Dim stream As New FileStream(strFile,
FileMode.OpenOrCreate)
stream.Write(bytFileByteArr, 0, bytFileByteArr.Length)
stream.Close()
Return 0
Catch ex As Exception
Return 1
End Try
Case ".tif"
' Get the file name and set a new
' path to the local storage folder
Dim strFile As String = _
System.IO.Path.GetFileNameWithoutExtension(sFileName)
strFile = System.Web.Hosting.HostingEnvironment.MapPath _
("~/StoredImages/" & strFile & sFileType)
'write the file out to the storage location
Try
Dim stream As New FileStream(strFile,
FileMode.OpenOrCreate)
stream.Write(bytFileByteArr, 0, bytFileByteArr.Length)
stream.Close()
Return 0
Catch ex As Exception
Return 1
End Try
Case ".ico"
' Get the file name and set a new
' path to the local storage folder
Dim strFile As String = _
System.IO.Path.GetFileNameWithoutExtension(sFileName)
strFile = System.Web.Hosting.HostingEnvironment.MapPath _
("~/StoredImages/" & strFile & sFileType)
'write the file out to the storage location
Try
Dim stream As New FileStream(strFile,
FileMode.OpenOrCreate)
stream.Write(bytFileByteArr, 0, bytFileByteArr.Length)
stream.Close()
Return 0
Catch ex As Exception
Return 1
End Try
Case ".wmf"
' Get the file name and set a new
' path to the local storage folder
Dim strFile As String = _
System.IO.Path.GetFileNameWithoutExtension(sFileName)
strFile = System.Web.Hosting.HostingEnvironment.MapPath _
("~/StoredImages/" & strFile & sFileType)
'write the file out to the storage location
Try
Dim stream As New FileStream(strFile,
FileMode.OpenOrCreate)
stream.Write(bytFileByteArr, 0, bytFileByteArr.Length)
stream.Close()
Return 0
Catch ex As Exception
Return 1
End Try
Case Else
' we don't know what kind of file it is
' so we put in into this folder
' Get the file name and set a new path to
' the local storage folder
Dim strFile As String = _
System.IO.Path.GetFileNameWithoutExtension(sFileName)
strFile = System.Web.Hosting.HostingEnvironment.MapPath _
("~/StorageStuff/" & strFile & sFileType)
'write the file out to the storage location
Try
Dim stream As New FileStream(strFile,
FileMode.OpenOrCreate)
stream.Write(bytFileByteArr, 0, bytFileByteArr.Length)
stream.Close()
Return 0
Catch ex As Exception
Return 1
End Try
End Select
End Function
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
' This subroutine will load a file directly to the site's server
' The file's extension is used to determine where to place
' the file (e.g., jpg, bmp, doc, txt, etc)
Try
' Validate that a file has been selected into the fileupload
control
If FileUpload1.HasFile Then
' Get the extension to determine the type of file uploaded
Dim sFileType As String
sFileType = System.IO.Path.GetExtension(FileUpload1.FileName)
sFileType = sFileType.ToLower()
' Process uploaded files by file type
Select Case sFileType
Case ".doc"
FileUpload1.SaveAs(MapPath("~/UploadedText/" & _
FileUpload1.FileName.ToString()))
Case ".docx"
FileUpload1.SaveAs(MapPath("~/UploadedText/" & _
FileUpload1.FileName.ToString()))
Case ".xml"
FileUpload1.SaveAs(MapPath("~/UploadedText/" & _
FileUpload1.FileName.ToString()))
Case ".rtf"
FileUpload1.SaveAs(MapPath("~/UploadedText/" & _
FileUpload1.FileName.ToString()))
Case ".lic"
FileUpload1.SaveAs(MapPath("~/UploadedText/" & _
FileUpload1.FileName.ToString()))
Case ".txt"
FileUpload1.SaveAs(MapPath("~/UploadedText/" & _
FileUpload1.FileName.ToString()))
Case ".html"
FileUpload1.SaveAs(MapPath("~/UploadedText/" & _
FileUpload1.FileName.ToString()))
Case ".htm"
FileUpload1.SaveAs(MapPath("~/UploadedText/" & _
FileUpload1.FileName.ToString()))
Case ".bmp"
FileUpload1.SaveAs(MapPath("~/UploadedPictures/" & _
FileUpload1.FileName.ToString()))
Case ".jpg"
FileUpload1.SaveAs(MapPath("~/UploadedPictures/" & _
FileUpload1.FileName.ToString()))
Case ".gif"
FileUpload1.SaveAs(MapPath("~/UploadedPictures/" & _
FileUpload1.FileName.ToString()))
Case ".png"
FileUpload1.SaveAs(MapPath("~/UploadedPictures/" & _
FileUpload1.FileName.ToString()))
Case ".tif"
FileUpload1.SaveAs(MapPath("~/UploadedPictures/" & _
FileUpload1.FileName.ToString()))
Case ".ico"
FileUpload1.SaveAs(MapPath("~/UploadedPictures/" & _
FileUpload1.FileName.ToString()))
Case ".wmf"
FileUpload1.SaveAs(MapPath("~/UploadedPictures/" & _
FileUpload1.FileName.ToString()))
Case Else
' we don't know it is so we put in into this folder
FileUpload1.SaveAs(MapPath("~/UploadedStuff/" & _
FileUpload1.FileName.ToString()))
End Select
MessageBox("File uploaded to web server.")
End If
Catch ex As Exception
MessageBox(ex.Message.ToString())
End Try
End Sub