Vba上传文本文件

时间:2015-03-17 18:52:47

标签: vba file-upload vbscript asp-classic winhttp

我试图将纯文本文件上传到经典的asp页面并收到错误。

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

Html代码

<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设置post变量并发送请求。

    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

我可以访问页面的源代码,这是一个经典的asp页面,上传是在服务器上运行的vbscript上执行的。

错误是File1参数=&#34;&#34;然后上传不会开始。

尽管html代码中的属性接受仅适用于&#34; image / jpeg&#34;我可以在浏览器导航时正常上传。

我认为我在Vba代码中的sPostData变量有问题。

包含上传功能的文件位于:https://www.royalholloway.ac.uk/resources/ASP/PStruh-CZ/1.3/upload.inc

任何人都可以看到我做错了什么?

2 个答案:

答案 0 :(得分:0)

我创建了上传页面的副本并查找了错误的内容。我发现页面重新合并的边界是"myboundary" + ;Charset=UTF-8

在这种情况下,解决方案是在请求标头中添加charset。

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&#34; Content-Type&#34;,&#34; multipart / form-data; Charset = UTF-8; boundary =&#34; &安培; STR_BOUNDARY

答案 1 :(得分:0)

今天我发现我可以通过标准POST从VBA Excel(也可以是Word,MSAccess或Outlook)上传文件(文本或二进制文件)(至少它适用于小文件)。 首先,它以字节数组打开一个文件。 其次,它将字节数组转换为Base64字符串。 第三,它使用IE对象来填充表单并提交它,而不需要直接的用户交互。

Public Function UploadFileTo(sourceFile As String, uURL As String, nameFieldID As String, dataFieldID As String, formID As String) As Boolean
    UploadFileTo = False
    ''---------------------------
    Dim nome As String, dados64
    Dim bs() As Byte
    nome = Dir(sourceFile)
    If (nome = "") Then Exit Function
    ''
    bs = ReadFile2("" & sourceFile) ' loads the file into a byte array
    dados64 = Base64Encode(bs) ' encodes it to base64 and returns as String
    ''
    Dim ie As InternetExplorerMedium
    'Dim ie 'As Object
    Dim docweb As Object
    Set ie = New InternetExplorerMedium
    'Set ie = CreateObject("InternetExplorerMedium")
    'Set ie = GetObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")
    ie.Navigate2 uURL
    ie.Visible = True
    While ie.Busy: DoEvents: Wend
    While ie.ReadyState <> 4: DoEvents: Wend
    ''
    Set docweb = ie.Document
    docweb.all(nameFieldID).Value = nome
    docweb.all(dataFieldID).Value = dados64
    docweb.all(formID).Submit
    While ie.Busy: DoEvents: Wend
    While ie.ReadyState <> READYSTATE_COMPLETE: DoEvents: Wend
    ''
    ie.Quit
    ''---------------------------
    UploadFileTo = True
End Function

例如:要调用以下页面:

if UploadFileTo("C:\myPath\myFile.txt", "http://www.website.com/page.html", "myFileNameField", "myFileData", "myDistantForm") then
    MsgBox "File Uploaded successfully"
else
    MsgBox "Failed."
end if

它会调用网址http://www.website.com/page.html

<html>
 <head>
  <title>Sending files within POST with base64 encoding</title>
 </head>
 <body>
  <form id="myDistantForm" action="teste.cgi" method="POST">
   Name: <input type="text" id="finame" name="arq_nome" value="">    <br>
   Data: <textarea name="myFileNameField" id="fidata" rows="30" cols="120"></textarea> <br>
   <input type="submit" name="myFileData" value="Send the File"> <br>
  </form>
 </body>
</html>

重要信息:服务器必须能够解码Base64编码的字符串,然后使用给定的名称(或某个自动生成的唯一名称)将解码的数据保存到所需服务器目录中的文件中,否无论目标服务器是运行ASP,PHP,JSP,CGI / ISAPI还是其他什么。

要将任何文件读取到字节数组:how can I read a binary file using VBA?

将该数组编码为Base64字符串:How do I base64 encode a string efficiently using Excel VBA?