1

I am trying to understand the file upload process using vba and winhttp.

I am trying to upload files to: https://uploadfiles.io/

Using the following code in VBA:

Public Function GetFileBytes(ByVal path As String) As Byte()
    Dim lngFileNum As Long
    Dim bytRtnVal() As Byte
    lngFileNum = FreeFile
    If LenB(Dir(path)) Then ''// Does file exist?
        Open path For Binary Access Read As lngFileNum
        ReDim bytRtnVal(LOF(lngFileNum) - 1&) As Byte
        Get lngFileNum, , bytRtnVal
        Close lngFileNum
    Else
        Err.Raise 53
    End If
    GetFileBytes = bytRtnVal
    Erase bytRtnVal
End Function

Sub testLoad()
Dim http
Dim filedata() As Byte

filedata = GetFileBytes("C:\apps\somefile.pdf")
Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
URL = "https://uploadfiles.io/upload"
http.Open "POST", URL, False

http.setRequestHeader "Content-Type", "multipart/form-data; boundary=---------------------------7e1881e4703b8" 'Add boundary
http.setRequestHeader "Content-Length", 80047 'Add length

http.send filedata

MsgBox http.Status
End Sub

I am a pretty big noob working with the web and winhttp. Using this code I get a successful 200 response... I think. But I have no idea where the file is now that it is uploaded. So here are my questions:

1.) Where and how do I set the file information as it is being uploaded?

2.) What exactly is the "boundary=" in the requestHeader? I manually set this by watching the network traffic, but I don't know what it means.

3.) What is the length in the requestHeader? Can I use len(filedata)

Any help would be greatly appreciated, thank you.

Seraphim
  • 171
  • 1
  • 12

0 Answers0