?
Solved

Upload Files using HTTP POST

Posted on 2005-03-01
15
Medium Priority
?
362 Views
Last Modified: 2010-05-02
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
Comment
Question by:kanishkpanwar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +1
15 Comments
 
LVL 32

Expert Comment

by:Erick37
ID: 13435778
This may help:

POSTing Form Data to a Web Page - Introduction
http://www.developerfusion.co.uk/show/3272/1/
0
 
LVL 28

Expert Comment

by:Ark
ID: 13436630
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
 

Author Comment

by:kanishkpanwar
ID: 13438940
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 28

Expert Comment

by:Ark
ID: 13445667
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
 
LVL 28

Expert Comment

by:Ark
ID: 13445672
PS:
pTargetURL = "http://hplg2.lenio.net/sync"
0
 
LVL 28

Expert Comment

by:Ark
ID: 13445743
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
 

Author Comment

by:kanishkpanwar
ID: 13448767
gives error @
      Body = Body & "&" & vNames(X) & "=" & urlEncode(vValues(X))

also... shud'nt the content type be multipart/form domething?
0
 

Author Comment

by:kanishkpanwar
ID: 13449395
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
 

Author Comment

by:kanishkpanwar
ID: 13451390
i got a working piece of code from the net. close this question please.
0
 
LVL 32

Expert Comment

by:Erick37
ID: 13451419
Can you please post the URL that you found so future readers can benefit?
0
 

Author Comment

by:kanishkpanwar
ID: 13452036
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
 

Author Comment

by:kanishkpanwar
ID: 13704725
sorry..
i was busy with other work. i need a refund on points. thank you.
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 14395777
PAQed with points refunded (500)

modulo
Community Support Moderator
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question