Introduction
This article shows how to use Web Services to create a service to manage (put and get) files in your Web Server using HTTP protocol.
If you don't have a physical access to the web server or just for sharing your web server content like pictures or files, this web service can be really useful and fast.
The code
So, let's stop talking and go to the code : )
First we need to import extra classes:
Imports System.Web.Services
Imports System.Configuration
Imports System.IO
A basic enumerator of files extensions:
Public Enum FileExtensions
htm
html
asp
aspx
jpg
gif
dll
exe
all
End Enum
The class FileInformation
will be populated with information about each file.
Public Class FileInformation
Public Name As String
Public Size As Long
Public CreadedDate As DateTime
Public LastModified As DateTime
Public LastAccess As DateTime
Public FileType As String
Public FileLocation As String
Public FileContent As Byte()
End Class
The WebMethod
Browse
will return an array of FileInformation
class:
<!---->
Public Function Browse(ByVal VirtualPath As String, ByVal FileExtension _
As FileExtensions) As FileInformation()
Dim i As Integer
Dim fi As FileInfo
Dim aFiles As FileInformation()
Dim mExtension As String
Select Case FileExtension
Case FileExtensions.asp
mExtension = "asp"
Case FileExtensions.aspx
mExtension = "aspx"
Case FileExtensions.gif
mExtension = "gif"
Case FileExtensions.htm
mExtension = "htm"
Case FileExtensions.html
mExtension = "html"
Case FileExtensions.jpg
mExtension = "jpg"
Case FileExtensions.dll
mExtension = "dll"
Case FileExtensions.exe
mExtension = "exe"
Case FileExtensions.all
mExtension = "*"
End Select
Dim di As New DirectoryInfo(WebServerPath & VirtualPath)
Dim afi As FileInfo() = _
di.GetFiles("*." & mExtension)
ReDim Preserve aFiles(afi.Length - 1)
For Each fi In afi
aFiles(i) = New FileInformation()
aFiles(i).Name = fi.Name
aFiles(i).Size = fi.Length
aFiles(i).LastAccess = fi.LastAccessTime
aFiles(i).CreadedDate = fi.CreationTime
aFiles(i).LastModified = fi.LastWriteTime
aFiles(i).FileType = fi.Extension
aFiles(i).FileLocation = fi.DirectoryName
aFiles(i).FileContent = ReadFile(WebServerPath & _
VirtualPath & "\" & fi.Name)
i += 1
Next
Return aFiles
End Function
The Private Shared
method ReadFile
returns a byte()
with the file content:
Private Shared Function ReadFile(ByVal FilePath As String) As Byte()
Dim fs As FileStream
Try
fs = File.Open(FilePath, FileMode.Open, FileAccess.Read)
Dim lngLen As Long = fs.Length
Dim abytBuffer(CInt(lngLen - 1)) As Byte
fs.Read(abytBuffer, 0, CInt(lngLen))
Return abytBuffer
Catch exp As Exception
Return Nothing
Finally
If Not fs Is Nothing Then
fs.Close()
End If
End Try
End Function
UploadFile
WebMethod
is used to send a single file to the web server:
<!---->
Public Function UploadFile(ByVal VirtualPath As String, _
ByVal Name As String, ByVal Content As Byte()) As Boolean
Dim objFile As File, objStream As StreamWriter, _
objFstream As FileStream
Try
objFstream = File.Open(WebServerPath & VirtualPath & "\" & _
Name, FileMode.Create, FileAccess.Write)
Dim lngLen As Long = Content.Length
objFstream.Write(Content, 0, CInt(lngLen))
objFstream.Flush()
objFstream.Close()
Return True
Catch exc As System.UnauthorizedAccessException
Return False
Catch exc As Exception
Return False
Finally
If Not objFstream Is Nothing Then
objFstream.Close()
End If
End Try
End Function
The WSFileServer
has another two useful methods called DirectoryExists
and FileExists
.
Using the code
There is a simple console application to retrieve a list of files. First create a new console application project and add a Web Reference to that:
Now add an import to System.IO
class, we need that for the save file method.
Imports System.IO
Add some private variables:
Private mWSFileServer As WSFileServer.FileServer
Private mFileInformation() As WSFileServer.FileInformation
Private mBar As New String("-", 50)
Public Const SaveFilePath As String = "C:\Temp\"
Now we'll create a method to save the file content, receiving the filename and file content:
Public Function SaveFile(ByVal Name As String, _
ByVal Content As Byte()) As Boolean
Dim objFstream As FileStream
Try
objFstream = File.Open(SaveFilePath & Name, _
FileMode.Create, FileAccess.Write)
Dim lngLen As Long = Content.Length
objFstream.Write(Content, 0, CInt(lngLen))
objFstream.Flush()
Return True
Catch exp As Exception
Return False
Finally
objFstream.Close()
End Try
End Function
The Sub
Main
method will initialize the file server Web service and show each file's specified web server virtual path:
Sub Main()
Try
mWSFileServer = New WSFileServer.FileServer()
mFileInformation = mWSFileServer.Browse("/ProgramGuide", _
WSFileServer.FileExtensions.aspx)
Dim i As Integer
For i = 0 To mFileInformation.Length - 1
With mFileInformation(i)
Console.WriteLine(mBar.ToString())
Console.WriteLine("File: {0}", .Name)
Console.WriteLine("Size: {0}", .Size)
Console.WriteLine("Location: {0}", .FileLocation)
Console.WriteLine("LastModified: {0}", .LastModified)
Console.WriteLine("CreateDate: {0}", .CreadedDate)
Console.WriteLine("LastAccess: {0}", .LastAccess)
If SaveFile(.Name, .FileContent) Then
Console.WriteLine("File saved sucessfull at:{0}", _
SaveFilePath & .Name)
Else
Console.WriteLine("Save file failure!")
End If
End With
Next
Console.WriteLine(mBar.ToString())
Console.Read()
Catch exp As Exception
Console.WriteLine(mBar)
Finally
mWSFileServer.Dispose()
End Try
End Sub
The result of the web service call can be seen in the following screen:
Console application calls the Browser
method of the web service, displays all files information and saves it.
Points of interest
I think that when you use Web Services technology, there are no limits for your applications, you just need to be creative ;-)
History
- 2/18/2003 - First release.