Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

POSTing a binary file and two html form vars to a lamp server using WinHttp.WinHttprequest

Status
Not open for further replies.

2shovels

Programmer
Jun 20, 2013
1
I've used this forum to get a lot of answers in the past but have just written a snippet i couldn't find anywhere on the net. Thanks to all you that share and keep this forum happening! I post it here, on this fine forum, in hopes of saving other coders time and hassle because there is not very much documentation on this. the code is simple but without documentation or samples was tricky for me to write. ie. the entire message body has to be CREATEBINARY if you are uploading any binary files, the formatting of the mime parts, boundaries and...well have a peak at the snippet... long live VFP!

* IMAGE TO COPY TO SERVER
lcImgName = "C:\test1.jpg"
* MIME PART BOUNDARY
lcBoundary = "XXXXX123456789XXXXXabcdefghijklmnopqrstuvwxyz"
* CAR. RETURN + LINE FEED
lcCrLf = CHR(13)+CHR(10)
* DOUBLE QUOTE "
lcQ = CHR(34)

oHTTP = CREATEOBJECT("WinHttp.WinHttprequest.5.1")
oHTTP.Open("POST", ' .F.)

* BUILD bodyStr STARTING WITH A FORM VAR NAMED username WITH A VALUE OF diane
bodyStr = "Content-Disposition: form-data; name=" + lcQ + "username" + lcQ + lcCrLf + lcCrLf +"diane" + lcCrLf
bodyStr = bodyStr + "--" + lcBoundary + lcCrLf

* LOAD BINARY FILE TO STRING
lcImgData = FILETOSTR(lcImgName)

* ADD BINARY FILE TRANSFER SETTINGS, NAME AND BINARY DATA TO bodyStr
bodyStr = bodyStr + "Content-Disposition: form-data; name=" +lcQ+ "Files1" + lcQ+ ";" +"filename="+lcQ+lcImgName+lcQ +lcCrLf
bodyStr = bodyStr + "Content-Type: image/jpeg" + lcCrLf + lcCrLf
bodyStr = bodyStr + lcImgData + lcCrLf + "--" + lcBoundary + lcCrLf

* ADD ANOTHER FORM VAR NAMED password VALUE aaaaa TO THE bodyStr
bodyStr = bodyStr + "Content-Disposition: form-data; name="+lcQ+"password"+lcQ+ lcCrLf + lcCrLf
bodyStr = bodyStr + "aaaaa" + lcCrLf + "--" + lcBoundary + lcCrLf

* MAKE THE ENTIRE BODY STRING BINARY SAFE
bodyStr = createbinary(bodyStr)

* SET THE HEADERS AND SEND THE MESSAGE BODY -bodyStr BUILT ABOVE
WITH oHTTP
.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1;)")
.setRequestHeader("Content-Type", "multipart/form-data; boundary=XXXXX123456789XXXXXabcdefghijklmnopqrstuvwxyz")
.setRequestHeader("Content-Length", Len(bodyStr))
.Send(bodyStr)
ENDWITH

* PRINT RESULTS FROM SERVER
? "Server Status = " + ALLTRIM(STR( oHTTP.status ))
? "SVR RESPONSE TEXT -> " + oHTTP.responseText
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top