|
-
Oct 10th, 2013, 06:35 AM
#1
winhttp file upload, using data from html file upload form
i posted this sample in vb6 earlier, but have decided it probably should, come into codebank
after several threads on automating file input elements in web page file upload forms,
after some searching here and on google, i have put together a simple example to upload files using winhttp, based on form in automated webbrowser or instance of IE
i only modified some code i found, all kudos to dulus http://www.access-programmers.co.uk/...d.php?t=155372
though i guess he got it from elsewhere anyway
the sample requires a form with file list (multi select) and command button
requires reference to winhttp
most of the code is in a small module
module
Code:
Function UploadXML(strformurl As String, strFileName As String, inputfile As String, Optional strUserName As String, Optional strPassword As String) As String
Const HTTPREQUEST_SETCREDENTIALS_FOR_SERVER = 0
Const HTTPREQUEST_SETCREDENTIALS_FOR_PROXY = 1
Dim WinHttpReq As WinHttp.WinHttpRequest
Dim strBody As String
Dim strFile As String
Dim aPostBody() As Byte
Dim bound As String
Dim boundSeparator As String
Dim boundFooter As String
bound = "AaB03x"
boundSeparator = "--" & bound & vbCrLf
boundFooter = "--" & bound & "--" & vbCrLf
Set WinHttpReq = New WinHttpRequest
WinHttpReq.Open "POST", strformurl, False
If strUserName <> "" And strPassword <> "" Then
WinHttpReq.SetCredentials strUserName, strPassword, HTTPREQUEST_SETCREDENTIALS_FOR_SERVER
End If
WinHttpReq.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & bound
strBody = boundSeparator
strBody = strBody & "Content-Disposition: form-data; name=""" & "login" & """" & vbCrLf & vbCrLf & strUserName
strBody = strBody & vbCrLf & boundSeparator
strBody = strBody & "Content-Disposition: form-data; name=""" & "pass" & """" & vbCrLf & vbCrLf & strPassword
strBody = strBody & vbCrLf & boundSeparator
strFile = getFile(strFileName)
strFileName = Right(strFileName, Len(strFileName) - InStrRev(strFileName, "\"))
' strBody = strBody & "Content-Disposition: form-data; name=""" & "userfile" & """; filename=""" & strFileName & """" & vbCrLf & _
strBody = strBody & "Content-Disposition: form-data; name=""" & inputfile & """; filename=""" & strFileName & """" & vbCrLf & _
"Content-Type: text/xml" & vbCrLf & vbCrLf & strFile & vbCrLf
strBody = strBody & boundFooter
'convert to byte array
aPostBody = StrConv(strBody, vbFromUnicode)
WinHttpReq.send aPostBody
' WinHttpReq.send strBody
Do Until WinHttpReq.Status = 200
DoEvents
Loop
UploadXML = WinHttpReq.responseText
Set WinHttpReq = Nothing
End Function
Function getFile(strFileName As String) As String
Dim strFile As String
Dim nFile
' Grap the file
nFile = FreeFile
Open strFileName For Binary As #nFile
strFile = String(LOF(nFile), " ")
Get #nFile, , strFile
Close #nFile
getFile = strFile
End Function
part of code in command1
Code:
Set frm = wb.document.Forms(0)
For Each ele In frm.getelementsbytagname("input")
If ele.Type = "file" Then fileinput = ele.Name
Next
formurl = frm.Action
For i = 0 To File1.ListCount - 1
If File1.Selected(i) Then res = UploadXML(formurl, File1.Path & "\" & File1.List(i), fileinput)
Next
all variables need to be dimensioned of appropriate types to be passed to functions
where wb is a webbrowser, instance of IE or shell window, that has already navigated to the file upload form document
the web document form /php script used for this test was of the simplest design and did not require any additional inputs to be posted with the file
if other hidden or user inputs need to be posted to the php script, both the name of the input and the value of the input must also be included with the post data, by passing them to the function and adding them to data, it may then be better to use a param array to pass the parameters to the function
everything depends on the requirements of the php script /server
i have successfully tested the code to upload files to my webserver
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|