' Collection of code snippets by Arne Vajhøj 
' (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
Imports System
Imports System.IO
Imports System.Net

Public Class FileUpload
    Private Const BOUNDARY As String = "ArneArne"
    Public Shared Sub Main(args As String())
        upload("http://localhost/upload.php", "C:\z.zip")
    End Sub
    Public Shared Sub upload(url As String, binfile As String)
        Dim req As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
        req.Method = "POST"
        req.ContentType = "multipart/form-data, boundary=" & BOUNDARY
        Dim post As New StreamWriter(req.GetRequestStream())
        post.WriteLine("--" & BOUNDARY)
        post.WriteLine("Content-disposition: form-data; name=""f""; filename=""" & Path.GetFileName(binfile) & """")
        post.WriteLine("Content-type: application/octet-stream")
        post.WriteLine("Content-Length: " & (New FileInfo(binfile)).Length)
        post.WriteLine("Content-Transfer-Encoding: binary")
        post.WriteLine("")
        post.Flush()
        Dim binf As New FileStream(binfile, FileMode.Open)
        Dim b(10000) As Byte
        Dim n As Integer
        n = binf.Read(b, 0, b.Length)
        While n > 0
            post.BaseStream.Write(b, 0, n)
            n = binf.Read(b, 0, b.Length)
        End While
        binf.Close()
        post.BaseStream.Flush()
        post.WriteLine()
        post.WriteLine("--" & BOUNDARY & "--")
        post.Close()
        Dim resp As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
        Console.WriteLine(resp.StatusCode)
        resp.Close()
    End Sub
End Class
