我使用启用宏的word文件。我需要在给定URL的情况下将文档保存在服务器上,并且在这里发布使用VBA代码的aspx页面就是我所做的。
'sends multipart/form-data To the URL using WinHttprequest/XMLHTTP
'FormData - binary (VT_UI1 | VT_ARRAY) multipart form data
Function WinHTTPPostRequest(URL, FormData, Boundary)
'Dim http As New MSXML2.XMLHTTP
'Create XMLHTTP/ServerXMLHTTP/WinHttprequest object
'You can use any of these three objects.
'Set http = CreateObject("WinHttp.WinHttprequest.5")
Set http = CreateObject("MSXML2.XMLHTTP")
'Set http = CreateObject("MSXML2.ServerXMLHTTP")
'Open URL As POST request
http.Open "POST", URL, False
'Set Content-Type header
'http.setRequestHeader "Content-Type", "multipart/form-data; boundary=" + Boundary
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
'Send the form data To URL As POST binary request
http.send (FormData)
'Get a result of the script which has received upload
MsgBox http.responseText
WinHTTPPostRequest = http.responseText
End Function
// --------------------------------------------- ----
Sub UploadFile(DestURL As String, FileName As String, Optional ByVal FieldName As String = "File")
DestURL = "http://192.168.1.41/s11/Journal.aspx?guid=fb492030-978f-4105-92f6-28a64959c612"
Dim sFormData As String, d As String
'Boundary of fields.
'Be sure this string is Not In the source file
Const Boundary As String = "---------------------------0123456789012sdsdssdsdsd"
'Get source file As a string.
sFormData = GetFile(FileName)
'Build source form with file contents
d = "--" + Boundary + vbCrLf
d = d + "Content-Disposition: form-data; name=""" + FieldName + """;"
d = d + " filename=""" + FileName + """" + vbCrLf
d = d + "Content-Type: application/upload" + vbCrLf + vbCrLf
d = d + sFormData
d = d + vbCrLf + "--" + Boundary + "--" + vbCrLf
'Post the data To the destination URL
WinHTTPPostRequest DestURL, sFormData, Boundary
End Sub
// --------------------------------------------- -----------
'read binary file As a string value
Function GetFile(FileName As String) As String
Dim FileContents() As Byte, FileNumber As Integer
ReDim FileContents(FileLen(FileName) - 1)
FileNumber = FreeFile
Open FileName For Binary As FileNumber
Get FileNumber, , FileContents
Close FileNumber
GetFile = StrConv(FileContents, vbUnicode)
End Function
// --------------------------------------------- -----------
Private Sub Document_Close()
UploadFile "", "E://Rd.docx"
End Sub
// --------------------------------------------- -----------
它发布到页面但我无法访问该文件?我肯定错过了什么 ?? 请查看附带示例代码Sample XML HTTP DOCUMENT SAVE
的附件答案 0 :(得分:0)
我意识到这个现在有点老了,而且你实际上并没有以真实的问题的形式表达这一点(不是过于谨慎,而是在陈述之后添加问号并不会自动提出问题!)。< / p>
然而,答案似乎相当简单:即使您在UploadFile中执行了各种步骤来设置变量d并提供正确的信息,然后您去传递sFormData(这只是GetFile的结果)
要纠正它,您应该在调用WinHTTPPostRequest之前替换该行:
sFormData = d + vbCrLf + "--" + Boundary + "--" + vbCrLf