Imports System.Web
Imports System.Web.Services
Imports System.IO

'Public Class hn_SimpleFileUploader
Public Class hn_SimpleFileUploader ':   IHttpHandler, System.Web.SessionState.IReadOnlySessionState
    Implements System.Web.IHttpHandler, System.Web.SessionState.IReadOnlySessionState

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

        context.Response.ContentType = "text/plain"
        Dim dirFullPath As String = HttpContext.Current.Server.MapPath("~/DocUploads/")
        Dim files As String()
        Dim numFiles As Int16
        Dim LastDotInFileName As Int16 = 0

        'String[] files;
        'Int numFiles;
        files = System.IO.Directory.GetFiles(dirFullPath)
        numFiles = files.Length
        numFiles = numFiles + 1

        Dim str_image As String = ""
        'String str_image = "";

        For Each s As String In context.Request.Files
            Dim file As HttpPostedFile = context.Request.Files(s)
            Dim fileName As String = file.FileName
            Dim fileExtension As String = file.ContentType
            Dim fileSize As String = file.ContentLength
            If Not fileName Is DBNull.Value Then
                fileExtension = Path.GetExtension(fileName)
                LastDotInFileName = fileName.LastIndexOf(".")
                If LastDotInFileName < 1 Then
                    LastDotInFileName = Len(fileName)
                End If
                str_image = Mid(fileName, 1, LastDotInFileName) & "_" & numFiles.ToString & fileExtension
                Dim pathToSave As String = HttpContext.Current.Server.MapPath("~/DocUploads/") & str_image
                file.SaveAs(pathToSave)

                For i = 0 To 24
                    If context.Session("FileUpload" & i) = "" Then
                        context.Session("FileUpload" & i) = str_image
                        Exit For
                    End If
                Next
            End If
        Next
        'context.Response.Write(str_image)
    End Sub

    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class