类模块文件
Option Explicit
Dim XMLhttp As MSXML2.XMLHTTP30
Dim XMLDoc As MSXML2.DOMDocument30
Dim WebURL As String
Dim XMLRootNode As VariantPrivate Sub Class_Initialize()
Set XMLhttp = New MSXML2.XMLHTTP30
Set XMLDoc = New MSXML2.DOMDocument30
Set Inet1 = New Inte
End SubPublic Sub Initialize()
XMLDoc.loadXML "<root/>"
Set XMLRootNode = XMLDoc.documentElement
End SubPublic Property Let SetWebURL(URL As String)
WebURL = URL
End PropertyPublic Sub AddaFile(filaPath As String, fileName As String)
Dim fileNum As Integer
Dim btArr() As Byte
Dim fileNameNode, fileContentNode, fileNode Set fileNode = XMLDoc.createNode(1, "file", "")
XMLRootNode.appendChild (fileNode)
Set fileNameNode = XMLDoc.createNode(1, "filename", "")
fileNameNode.Text = fileName
fileNode.appendChild (fileNameNode)
Set fileContentNode = XMLDoc.createNode(1, "fileContent", "")
fileNode.appendChild (fileContentNode)
fileNum = FreeFile
Open filaPath & fileName For Binary Access Read As #fileNum
ReDim btArr(LOF(fileNum))
Get #fileNum, , btArr()
Close #fileNum
With fileContentNode
.dataType = "bin.base64"
.nodeTypedValue = btArr
End With
End SubPublic Function HttpUpload() As Boolean
Dim strHeaders As String
HttpUpload = False
XMLhttp.open "POST", WebURL, False
XMLhttp.send XMLDoc Debug.Print XMLhttp.responseText
If XMLhttp.responseText = "Upload successful!" Then
HttpUpload = True
End If
End FunctionPrivate Sub Class_Terminate()
Set XMLhttp = Nothing
Set XMLDoc = Nothing
End Sub
窗体文件
Private Sub Command1_Click()
Dim filepath As String
Dim fileName As String
Dim WebURL As String
WebURL = "http://localhost/upload.asp"
'Set Inet1 = New Inte
filepath = "F:\"
fileName = "1.jpg"
Dim XMLhttp As ClsXMLhttp.Initialize
XMLhttp.SetWebURL = WebURL
XMLhttp.AddaFile filepath, fileName
XMLhttp.HttpUpload
End Sub运行错误:对象变量或with变量未设置//这里报错啦 XMLhttp.Initialize
Option Explicit
Dim XMLhttp As MSXML2.XMLHTTP30
Dim XMLDoc As MSXML2.DOMDocument30
Dim WebURL As String
Dim XMLRootNode As VariantPrivate Sub Class_Initialize()
Set XMLhttp = New MSXML2.XMLHTTP30
Set XMLDoc = New MSXML2.DOMDocument30
Set Inet1 = New Inte
End SubPublic Sub Initialize()
XMLDoc.loadXML "<root/>"
Set XMLRootNode = XMLDoc.documentElement
End SubPublic Property Let SetWebURL(URL As String)
WebURL = URL
End PropertyPublic Sub AddaFile(filaPath As String, fileName As String)
Dim fileNum As Integer
Dim btArr() As Byte
Dim fileNameNode, fileContentNode, fileNode Set fileNode = XMLDoc.createNode(1, "file", "")
XMLRootNode.appendChild (fileNode)
Set fileNameNode = XMLDoc.createNode(1, "filename", "")
fileNameNode.Text = fileName
fileNode.appendChild (fileNameNode)
Set fileContentNode = XMLDoc.createNode(1, "fileContent", "")
fileNode.appendChild (fileContentNode)
fileNum = FreeFile
Open filaPath & fileName For Binary Access Read As #fileNum
ReDim btArr(LOF(fileNum))
Get #fileNum, , btArr()
Close #fileNum
With fileContentNode
.dataType = "bin.base64"
.nodeTypedValue = btArr
End With
End SubPublic Function HttpUpload() As Boolean
Dim strHeaders As String
HttpUpload = False
XMLhttp.open "POST", WebURL, False
XMLhttp.send XMLDoc Debug.Print XMLhttp.responseText
If XMLhttp.responseText = "Upload successful!" Then
HttpUpload = True
End If
End FunctionPrivate Sub Class_Terminate()
Set XMLhttp = Nothing
Set XMLDoc = Nothing
End Sub
窗体文件
Private Sub Command1_Click()
Dim filepath As String
Dim fileName As String
Dim WebURL As String
WebURL = "http://localhost/upload.asp"
'Set Inet1 = New Inte
filepath = "F:\"
fileName = "1.jpg"
Dim XMLhttp As ClsXMLhttp.Initialize
XMLhttp.SetWebURL = WebURL
XMLhttp.AddaFile filepath, fileName
XMLhttp.HttpUpload
End Sub运行错误:对象变量或with变量未设置//这里报错啦 XMLhttp.Initialize
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货