Search code examples
vbafile-uploadvbscriptasp-classicwinhttp

Vba Upload Text File


I'm trying to upload a plain text file to a classic asp page and getting an error.

<p>Microsoft VBScript runtime </font> <font face="Arial" size=2>error '800a01a8'</font>
<p>
<font face="Arial" size=2>Object required: 'fields(...)'</font>

Html Code

<form action="upload.asp" method=post ENCTYPE="multipart/form-data">
      File :
<input type="file" class="form_field" name="File1" accept="image/jpeg">
<input type="submit" class="form_pb" Name="Action" value="Send File">
</form>

Vba Code to set the post variable and send the request.

    Const STR_BOUNDARY  As String = "a832972453175"
    Dim nFile           As Integer
    Dim baBuffer()      As Byte
    Dim sPostData       As String
....

'--- read file
nFile = FreeFile
Open sFileName For Binary Access Read As nFile
    If LOF(nFile) > 0 Then
        ReDim baBuffer(0 To LOF(nFile) - 1) As Byte
        Get nFile, , baBuffer
        sPostData = StrConv(baBuffer, vbUnicode)
    End If
Close nFile

sPostData = "--" & STR_BOUNDARY & vbCrLf & _
        "Content-Disposition: form-data; name=""File1""; filename=""" & Mid$(sFileName, InStrRev(sFileName, "\") + 1) & """" & vbCrLf & _
            "Content-Type: text/plain" & vbCrLf & vbCrLf & _
            sPostData & vbCrLf & vbCrLf & _
            STR_BOUNDARY & vbCrLf & _
            "Content-Disposition: form-data; name=""Action""" & vbCrLf & _
             vbCrLf & "Send File" & vbCrLf & _
            "--" & STR_BOUNDARY & "--"

With WinHttpReq

'UPLOAD REQUEST
.Open "POST", sUrl, False
.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & STR_BOUNDARY
.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
.Send (sPostData)
end with

I have access to the source code of the page that is a classic asp page and the upload is executed with a vbscript running at the server.

The error is that the File1 argument = "" and then the upload doesnt start.

Despite the attribute accept in the html code is only for "image/jpeg" I can upload normally when navigating with a browser.

I think is something wrong with my sPostData variable in Vba code.

The file with the upload functions is here: https://www.royalholloway.ac.uk/resources/ASP/PStruh-CZ/1.3/upload.inc

Anyone can see what am I doing wrong?


Solution

  • I created a copy of the upload page and looked for something wrong. I found out that the boundary reconigzed by the page was "myboundary" + ;Charset=UTF-8.

    The solution in this case is add the charset in the request header.

    With WinHttpReq
    
    'UPLOAD REQUEST
    .Open "POST", sUrl, False
    .setRequestHeader "Content-Type", "multipart/form-data; Charset=UTF-8;boundary=" & STR_BOUNDARY
    .setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
    .Send (sPostData)
    end with
    

    .setRequestHeader "Content-Type", "multipart/form-data; Charset=UTF-8; boundary=" & STR_BOUNDARY