• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 367
  • Last Modified:

Upload Files using HTTP POST

this is the code for html PAGE
------------------------
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
      PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US"><head><title>sample file upload form</title>
</head><body><h2>Upload files to http://hplg2.lenio.net/sync</h2><form method="post" action="http://hplg2.lenio.net/sync" enctype="multipart/form-data">
<input type="hidden" name="fileUpload" value="1"  /><input type="hidden" name="Property_ID" value="EA"  /><p>PDF: <input type="file" name="PDF" value="/tmp/somepdffile.pdf" size="50" maxlength="80" /></p><p>XML: <input type="file" name="XML" value="/tmp/somexmlfile.xml" size="50" maxlength="80" /></p><input type="submit" name=".submit" /><div></div>
</form></body></html>
-------------------------------------------------------------

i need to emulate this page behavior using VB6.
i need to upload 2 files. may be an extra field or 2 as well. I need to DO IT AS AN HTTP POST REQUEST. THIS IS A VERYVERY URGENT SITUATION !!!
0
kanishkpanwar
Asked:
kanishkpanwar
  • 6
  • 4
  • 2
  • +1
1 Solution
 
Erick37Commented:
This may help:

POSTing Form Data to a Web Page - Introduction
http://www.developerfusion.co.uk/show/3272/1/
0
 
ArkCommented:
Private Sub Form_Load()
   WebBrowser1.Navigate "about:blank"
   Dim sBody As String
   sBody = "<h2>Upload files to http://hplg2.lenio.net/sync</h2>"
   sBody = sBody & "<form method=""post"" action=""http://hplg2.lenio.net/sync"" enctype=""multipart/form-data"">"
   sBody = sBody & "<input type=""hidden"" name=""fileUpload"" value=""1""  />"
   sBody = sBody & "<input type=""hidden"" name=""Property_ID"" value=""EA""  />"
   sBody = sBody & "<p>PDF:<input type=""file"" name=""PDF"" value=""/tmp/somepdffile.pdf"" size=""50"" maxlength=""80"" /></p>"
   sBody = sBody & "<p>XML: <input type=""file"" name=""XML"" value=""/tmp/somexmlfile.xml"" size=""50"" maxlength=""80"" /></p>"
   sBody = sBody & "<input type=""submit"" name="".submit"" /><div></div></form>"
   While WebBrowser1.ReadyState < READYSTATE_COMPLETE
      DoEvents
   Wend
   WebBrowser1.Document.body.innerHTML = sBody
End Sub
0
 
kanishkpanwarAuthor Commented:
i am looking for a way to call HTTP POST on the URL to upload 2 files from local system and a 2 parameters name=""fileUpload"" value=""1""   and name=""Property_ID"" value=""EA""  .
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ArkCommented:
Hi
Add referense to Microsoft XML.

Public Function Post() As String
   Dim httpObj As MSXML.XMLHTTPRequest
   Dim Body As String
   
   Set httpObj = New MSXML.XMLHTTPRequest
   
   Call httpObj.open("POST", pTargetURL, False)
   Call httpObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
   
   ' Build the body by joining all the names with their encoded values
   Body = "fileUpload=1&Property_ID=EA"  
   
   Call httpObj.send(Body)
   
   Post = httpObj.responseText
   
   Set httpObj = Nothing
End Function

0
 
ArkCommented:
PS:
pTargetURL = "http://hplg2.lenio.net/sync"
0
 
ArkCommented:
Oops, forgot about files:

Public Function Post() As String
   Dim httpObj As MSXML.XMLHTTPRequest
   Dim X As Long
   Dim Body As String
   Dim vNames As Variant, vValues As Variant

   vNames = Array("fileUpload", "Property_ID", "PDF", "XML")
   vValues = Array("1", "EA", "/tmp/somepdffile.pdf", "/tmp/somepdffile.xml"
   Set httpObj = New MSXML.XMLHTTPRequest
   
   Call httpObj.open("POST", pTargetURL, False)
   Call httpObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
   
   ' Build the body by joining all the names with their encoded values
   For X = 0 To UBound(vNames)
      Body = Body & "&" & vNames(X) & "=" & urlEncode(vValues(X))
   Next X
   
   Body = Mid$(Body, 2) ' Remove the extra & symbol
   
   Call httpObj.send(Body)
   
   Post = httpObj.responseText
   
   Set httpObj = Nothing
End Function


Private Function urlEncode(EncodeString As String) As String
    'This function recieves a string and returns it
    'in encoded, url friendly form
    Dim returnString As String
    Dim currentChar As String
    Dim I As Long

    For I = 1 To Len(EncodeString)
        currentChar = Mid(EncodeString, I, 1)

        If Asc(currentChar) < 91 And Asc(currentChar) > 64 Then
            'character is between A to Z so leave it
            '
            returnString = returnString + currentChar
        ElseIf Asc(currentChar) < 123 And Asc(currentChar) > 96 Then
            'character is between a to z so leave it
            '
            returnString = returnString + currentChar
        ElseIf Asc(currentChar) < 58 And Asc(currentChar) > 47 Then
            'character is between 0 to 9 so leave it
            '
            returnString = returnString + currentChar
        ElseIf Asc(currentChar) = 32 Then
            returnString = returnString + "+"
            'character is a space so change it to a
            '     plus
        Else
            'character needs a hex conversion
            returnString = returnString + "%" + Hex(Asc(currentChar))
        End If
    Next
    'Return the url encoded string
    urlEncode = returnString
End Function
0
 
kanishkpanwarAuthor Commented:
gives error @
      Body = Body & "&" & vNames(X) & "=" & urlEncode(vValues(X))

also... shud'nt the content type be multipart/form domething?
0
 
kanishkpanwarAuthor Commented:
it does not work.
if u try and read teh response thext then there is no success message which is expected.

save the HTML code in my title post and try to upload file from the page. u'll see what i mean.
0
 
kanishkpanwarAuthor Commented:
i got a working piece of code from the net. close this question please.
0
 
Erick37Commented:
Can you please post the URL that you found so future readers can benefit?
0
 
kanishkpanwarAuthor Commented:
sure thing :)

http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=6076&lngWId=1

i had to modify my request header a little.
0
 
kanishkpanwarAuthor Commented:
sorry..
i was busy with other work. i need a refund on points. thank you.
0
 
moduloCommented:
PAQed with points refunded (500)

modulo
Community Support Moderator
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now