问题
I use an Macro enabled word file. I need to save the document on the server given an URL and an aspx page to post to using VBA code here is what i have done .
'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
//--------------------------------------------------------
It posts to the page but i am not able to access the file ? I must be missing something ?? Please find attached document with the sample code Sample XML HTTP DOCUMENT SAVE
回答1:
I realise this is a little old now and in addition you haven't actually expressed this in the form of a genuine question (not to be persnickety, but adding question marks after statements do not automatically make them questions!).
However the answer seems fairly simple: even though you do various steps in UploadFile to set up the variable d with the right information to submit, you then go and pass sFormData (which is just the result of GetFile)
To rectify it, you should replace the line before the call to WinHTTPPostRequest with:
sFormData = d + vbCrLf + "--" + Boundary + "--" + vbCrLf
来源:https://stackoverflow.com/questions/20520212/post-file-to-server-asp-net-using-xmlhttp