Hello everyone, first, thanks for taking the time too look at my post. Any help anyone can give would be great.
I'm trying to
upload a file to a server through HTTP, but I'm having some problems.
We were able to upload a txt file without using a username and password, but
once we set up the server to use credentials, and tried the form again, it's giving us an error. I've aslo attached a word doc with the current code and a screen shot. If you don't mind taking a look at it, I would greatly appreciate it. I'll past the code here too without the screen shot.
Error that is coming from the form:
"The Server returned an invalid or unrecognized response."
Error coming from the server side is:
"Invalid method in request –-Xu02=$"
(I omitted username, password, and server location)
Code:
----------------------------FORM CODE-----------------------------------
Private Sub Command1_Click()
Dim iArray
'Set your filename to upload here
iArray = Array("C:\Documents and Settings\rdstephens\Desktop\Upload.txt")
'Set path to php upload script here
Text1.Text = UploadFiles(iArray, "--------------Server Address--------------", Text2.Text, "******", "******")
End Sub
----------------------------SCRIPT CODE------------------------------------
'You need reference to Microsoft WinHTTP Services 5.0 or 5.1 to use this example
'Credit to Joseph Z. Xu ()
'Modified by Mohd Idzuan Alias () August 18, 2004
'Modified by Kung Fu Panda, Wizard of Warcraft, and The Intern, June 19, 2008
Dim WinHttpReq As WinHttp.WinHttpRequest
Const HTTPREQUEST_SETCREDENTIALS_FOR_SERVER = 0
Const HTTPREQUEST_SETCREDENTIALS_FOR_PROXY = 1
Const BOUNDARY = "Xu02=$"
Const HEADER = "--Xu02=$"
Const FOOTER = "--Xu02=$--"
Function UploadFiles(strFileName As Variant, strURL As String, Optional postVar As String, _ Optional strUserName As String, Optional strPassword As String) As String
Dim fname As String
Dim strFile As String
Dim strBody As String
Dim aPostBody() As Byte
Dim nFile As Integer
Set WinHttpReq = New WinHttpRequest
' Turn error trapping on
On Error GoTo SaveErrHandler
' Assemble an HTTP request.
strURL = strURL & "?slots=" & CStr(UBound(strFileName) + 1) & "&" & postVar
WinHttpReq.Open "POST", strURL, False
Debug.Print strURL
If strUserName <> "" And strPassword <> "" Then
' Set the user name and password.
WinHttpReq.SetCredentials strUserName, strPassword, _
HTTPREQUEST_SETCREDENTIALS_FOR_SERVER
End If
'-------------------------- Becareful not to mingle too much here -----------------------------------
' Set the header
WinHttpReq.SetRequestHeader "Content-Type", "multipart/form-data; boundary=" & BOUNDARY
' Assemble the body
strBody = HEADER ' Starting tag
For i = 0 To UBound(strFileName)
' Grap the name
fname = strFileName(i)
' Grap the file
strFile = getFile(fname)
strBody = strBody & vbCrLf & "Content-Disposition: form-data; name=""" & "name" & """; filename=""" & fname & """ " & vbCrLf & "Content-type: file" & _
vbCrLf & vbCrLf & strFile & vbCrLf
If i < UBound(strFileName) Then
strBody = strBody & "--Xu02=$" ' This is boundary tag between two files
End If
strFile = ""
Next i
strBody = strBody & FOOTER ' Ending tag
'----------------------------------------------------------------------------------------------------
' Because of binary zeros, post body has to convert to byte array
aPostBody = StrConv(strBody, vbFromUnicode)
' Send the HTTP Request.
WinHttpReq.Send aPostBody
' Display the status code and response headers.
'UploadFiles = WinHttpReq.GetAllResponseHeaders & " " & WinHttpReq.ResponseText
UploadFiles = WinHttpReq.ResponseText
Set WinHttpReq = Nothing
Exit Function
SaveErrHandler:
UploadFiles = Err.Description
Set WinHttpReq = Nothing
End Function
Function getFile(strFileName As String) As String
Dim strFile As String
' Grap the file
nFile = FreeFile
Open strFileName For Binary As #nFile
strFile = String(LOF(nFile), " ")
Get #nFile, , strFile
Close #nFile
getFile = strFile
End Function
Last edited by RobDog888; Jun 25th, 2008 at 07:21 PM.