3

我正在使用 Visual Basic 6,并希望通过发送自定义输入字段和 PDF 文件来对服务器(它运行 Java 代码)进行 HTTP POST。PDF 文件必须是基本 64 位编码的,或者在上传文件时使用 HTTP POST 在 Internet 上工作的正常方式。基本上,我只想从我的 Visual Basic 6 程序上传一个文件。

我该怎么做呢?任何示例源代码?

4

2 回答 2

3

假设您知道如何将 PDF 加载到字节数组中,您必须对其进行 Base64 编码,然后使用 MIME 多部分编码将其发布到服务器。

您可以利用 MSXML 库功能执行 Base64 编码。有关详细信息,请参阅此链接

将 PDF 作为 Bas64 字符串后,您需要将其打包为 MIME 多部分。您可以使用 MSXML 中的 XMLHTTP 对象为您执行该发布:-

sEntityBody = "----boundary" & vbCrLf
sEntityBody = sEntityBody & "Content-Disposition: form-data; name=fileInputElementName; filename=""" + sFileName + """" & vbCrLf
sEntityBody = sEntityBody & "Content-Transfer-Encoding: base64" & vbCrLf
sEntityBody = sEntityBody & "Content-Type: application/pdf" &  vbCrLf & vbCrLf
sEntityBody = sEntityBody & sPDFBase64 & vbCrLf
sEntityBody = sEntityBody & "-----boundary--" & vbCrLf & vbCrLf

Set xhr = New MSXML2.XMLHTTP30
xhr.setRequestHeader("Content-Type", "multipart/form-data; boundary=-----boundary")
xhr.Open "POST", sUrl, False
xhr.send sEntityBody

也许不是优雅或高效,但它应该可以工作。

于 2009-06-09T11:05:53.033 回答
1

这是处理base 64的代码

Private Declare Function CryptBinaryToString Lib "Crypt32.dll" Alias "CryptBinaryToStringW" (ByRef pbBinary As Byte, ByVal cbBinary As Long, ByVal dwFlags As Long, ByVal pszString As Long, ByRef pcchString As Long) As Long
Private Declare Function CryptStringToBinary Lib "Crypt32.dll" Alias "CryptStringToBinaryW" (ByVal pszString As Long, ByVal cchString As Long, ByVal dwFlags As Long, ByVal pbBinary As Long, ByRef pcbBinary As Long, ByRef pdwSkip As Long, ByRef pdwFlags As Long) As Long

Public Function Base64Decode(sBase64Buf As String) As String
    Const CRYPT_STRING_BASE64 As Long = 1
    Dim bTmp() As Byte, lLen As Long, dwActualUsed As Long
    If CryptStringToBinary(StrPtr(sBase64Buf), Len(sBase64Buf), CRYPT_STRING_BASE64, StrPtr(vbNullString), lLen, 0&, dwActualUsed) = 0 Then Exit Function       'Get output buffer length
    ReDim bTmp(lLen - 1)
    If CryptStringToBinary(StrPtr(sBase64Buf), Len(sBase64Buf), CRYPT_STRING_BASE64, VarPtr(bTmp(0)), lLen, 0&, dwActualUsed) = 0 Then Exit Function 'Convert Base64 to binary.
    Base64Decode = StrConv(bTmp, vbUnicode)
End Function

Public Function Base64Encode(Text As String) As String
    Const CRYPT_STRING_BASE64 As Long = 1
    Dim lLen As Long, m_bData() As Byte, sBase64Buf As String
    m_bData = StrConv(Text, vbFromUnicode)
    If CryptBinaryToString(m_bData(0), UBound(m_bData) + 1, CRYPT_STRING_BASE64, StrPtr(vbNullString), lLen) = 0 Then Exit Function  'Determine Base64 output String length required.
    sBase64Buf = String$(lLen - 1, Chr$(0)) 'Convert binary to Base64.
    If CryptBinaryToString(m_bData(0), UBound(m_bData) + 1, CRYPT_STRING_BASE64, StrPtr(sBase64Buf), lLen) = 0 Then Exit Function
    Base64Encode = sBase64Buf
End Function
于 2021-06-03T03:03:35.957 回答