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

VB6 - using POST to send a binary file to a server.

What's the best VB6 method to send a binary file to an asp page using the POST method?  Preferably using any of VB's standard set of ActiveX controls.

I have an outstanding question in the ASP section also -  how to write the .asp part of the destination.

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/ASP/Q_22889200.html
0
peetm
Asked:
peetm
  • 10
  • 5
1 Solution
 
MELeBlancCommented:
This probably will not give you exactly what you need... but it was enough information for us to be able to convert the JavaScript over to VB.

http://www.15seconds.com/issue/010522.htm

-M
0
 
MELeBlancCommented:
Actually... I just realized that it may also answer your question over in ASP as well  ;-)

-M
0
 
peetmAuthor Commented:
Ok, converted to VB, and have this is a form ...

Private Sub Command1_Click()

   ' create ADO-stream Object
   Dim ado_stream As Object
   
   Set ado_stream = CreateObject("ADODB.Stream")

   ' create XML document with default header and primary node
   Dim xml_dom As Object
   
   Set xml_dom = CreateObject("MSXML2.DOMDocument")
   
   Call xml_dom.loadXML("<?xml version=" & Chr$(34) & "1.0" & Chr$(34) & " ?> <root/>")
   
   ' specify namespaces datatypes
   Call xml_dom.documentElement.setAttribute("xmlns:dt", "urn:schemas-microsoft-com:datatypes")

   ' create a new node and set binary content
   
   Dim l_node1 As Object
   
   Set l_node1 = xml_dom.createElement("file1")
   
   l_node1.dataType = "bin.base64"
   
   ' open stream object and read source file
   ado_stream.Type = 1  ' 1=adTypeBinary
   Call ado_stream.open
   Call ado_stream.LoadFromFile("c:\junk\sub_rss.gif")
   
   ' store file content into XML node
   l_node1.nodeTypedValue = ado_stream.Read(-1) ' -1=adReadAll
   
   Call ado_stream.Close
   Call xml_dom.documentElement.appendChild(l_node1)

   ' we can create more XML nodes for multiple file upload

   ' send XML document to Web server
   Dim xmlhttp As Object
   
   Set xmlhttp = CreateObject("Microsoft.XMLHTTP")
   
   Call xmlhttp.open("POST", "http://www.peetm.com/file_receive.asp", False)
   Call xmlhttp.send(xml_dom)  ' ********
   
   ' show server message in message-area
   Debug.Print xmlhttp.responseText
   
End Sub


And have this on my server in file_receive.asp

<%@ LANGUAGE=VBScript%>
<% Option Explicit
   Response.Expires = 0
   
   ' define variables and COM objects
   dim ado_stream
   dim xml_dom
   dim xml_file1

   ' create Stream Object
   set ado_stream = Server.CreateObject("ADODB.Stream")
   ' create XMLDOM object and load it from request ASP object
   set xml_dom = Server.CreateObject("MSXML2.DOMDocument")
   xml_dom.load(request)
   ' retrieve XML node with binary content
   set xml_file1 = xml_dom.selectSingleNode("root/file1")

   ' open stream object and store XML node content into it  
   ado_stream.Type = 1  ' 1=adTypeBinary
   ado_stream.open
   ado_stream.Write xml_file1.nodeTypedValue
   ' save uploaded file
   ado_stream.SaveToFile "upload1.doc",2  ' 2=adSaveCreateOverWrite
   ado_stream.close

   ' destroy COM object  
   set ado_stream = Nothing
   set xml_dom = Nothing
   ' write message to browser
   Response.Write "Upload successful!"
%>

In the client I get an error on the line marked ' ******** - the error is a non-helpful 'The parameter is incorrect'
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
MELeBlancCommented:
While eliminating the CreateObject code in the VB part I found the issue.. I believe it is the fact that the xmlhttp is a type.. so here is the "new" code:

   ' create ADO-stream Object
   Dim ado_stream As Object
   
   Set ado_stream = CreateObject("ADODB.Stream")

   ' create XML document with default header and primary node
   Dim xml_dom As New MSXML2.DOMDocument
   
   Call xml_dom.loadXML("<?xml version=" & Chr$(34) & "1.0" & Chr$(34) & " ?> <root/>")
   
   ' specify namespaces datatypes
   Call xml_dom.documentElement.setAttribute("xmlns:dt", "urn:schemas-microsoft-com:datatypes")

   ' create a new node and set binary content
   
   Dim l_node1 As Object
   
   Set l_node1 = xml_dom.createElement("file1")
   
   l_node1.dataType = "bin.base64"
   
   ' open stream object and read source file
   ado_stream.Type = 1  ' 1=adTypeBinary
   Call ado_stream.open
   Call ado_stream.LoadFromFile("d:\icon_swap_right.gif")
   
   ' store file content into XML node
   l_node1.nodeTypedValue = ado_stream.Read(-1) ' -1=adReadAll
   
   Call ado_stream.Close
   Call xml_dom.documentElement.appendChild(l_node1)

   ' we can create more XML nodes for multiple file upload

   ' send XML document to Web server
   Dim objxmlhttp As New xmlhttp
   
   
   Call objxmlhttp.open("POST", "http://melinteg/iqsp/test.asp", False)
   objxmlhttp.send xml_dom
   
   ' show server message in message-area
   Debug.Print objxmlhttp.responseText


I am, however, getting a 500 error on the ASP side... so I am still looking at that.

-M
0
 
MELeBlancCommented:

The line that is failing is the actual write of the file...

ADODB.Stream (0x800A0BBC)
Write to file failed.

I am thinking that it might be a permissions issue but have no way of checking at the moment.

-M
0
 
MELeBlancCommented:


While I am at it... while looking around for some answers I also ran across this:

http://www.perfectxml.com/msxmlAnswers.asp?Row_ID=60

0
 
MELeBlancCommented:

Yep... permissions problems.

Went into IIS and gave the site Write persmissions and then changed the directory security by adding Everyone with Full Control.
This is definitely overkill.. but I just took the wide sweeping path to ascertain if it was indeed a permissions problem.

-M
0
 
peetmAuthor Commented:
>>The line that is failing is the actual write of the file...

>>ADODB.Stream (0x800A0BBC)
>>Write to file failed.

If you try it with my page: http://www.peetm.com/file_recieve.asp you'll see it gets a 404 back.

However, the page is there - witnessed by the fact that a direct navigation to it returns this:

Microsoft VBScript runtime  error '800a01a8'

Object required

/file_recieve.asp, line 21
0
 
MELeBlancCommented:
Actually.. I get the object required error when I change the URL to yours.

Are the MSXML components installed on your web server as well?

-M
0
 
MELeBlancCommented:
BTW.. here is a copy (for what it is worth) of my catch asp page:

<%@ language="VBSCRIPT" %>
<html>
<head>
<meta name="GENERATOR" content="SAPIEN Technologies PrimalScript 3.1">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Document Title</title>
</head>
<body>

<%
   Response.Expires = 0
   
   ' define variables and COM objects
   dim ado_stream
   dim xml_dom
   dim xml_file1

   ' create Stream Object
   set ado_stream = Server.CreateObject("ADODB.Stream")
   ' create XMLDOM object and load it from request ASP object
   set xml_dom = Server.CreateObject("MSXML2.DOMDocument")
   xml_dom.load(request)
   ' retrieve XML node with binary content
   set xml_file1 = xml_dom.selectSingleNode("root/file1")

   ' open stream object and store XML node content into it  
   ado_stream.Type = 1  ' 1=adTypeBinary
   ado_stream.open
   ado_stream.Write xml_file1.nodeTypedValue
   ' save uploaded file
   ado_stream.SaveToFile "d:\websites\wwwsql\temp.gif",2  ' 2=adSaveCreateOverWrite
   ado_stream.close

   ' destroy COM object  
   set ado_stream = Nothing
   set xml_dom = Nothing
   ' write message to browser
   Response.Write "Upload successful!"
%>

</body>
</html>


I only made a few minor changes to it.. not sure if it was needed but I gave the full path to the directory (just to be sure).

-M
0
 
peetmAuthor Commented:
>>Are the MSXML components installed on your web server as well?

Sorry but I don't  know - I can raise a ticket with my ISP and ask of course - that's ixwebhosting btw.  Is there any other way to tell?

If I put a few Response.Write "Here" type calls in, it seems to me that the erroring line is:

ado_stream.Write xml_file1.nodeTypedValue

Would you like me to upload your 'catch' page to my website?

BTW - thanks!
0
 
peetmAuthor Commented:
I've uploaded your asp page to my server, it's called file_recieve1.asp.

The only thing I changed was

     ado_stream.SaveToFile ".\temp.gif",2  ' 2=adSaveCreateOverWrite

chmod shows its permissions as 777

0
 
MELeBlancCommented:

When I point to the file_receive1.asp I get:

Write to file failed.
/file_recieve1.asp, line 32


so it definitely sounds like a persmissions issue.

Since you are working with an ISP I am not sure how compliant they are going to be with making the required changes on permissions since I believe this needs to have both IIS configuration changes and the changes made to the directory itself.  The directory at least would have to have write permissions for the account that the website is running under... usually an IUSR_ account.

It also does not appear to like the SaveToFile unless you give it the full path... so I made some changes to the code and it will now build the path for you.

   Dim strOutPath
   strOutPath = Server.MapPath(Request.ServerVariables("PATH_INFO"))
   strOutPath = Left(strOutPath,InStrRev(strOutPath,"\")-1)
   ado_stream.SaveToFile strOutPath & "/temp.gif",2  ' 2=adSaveCreateOverWrite


-M
0
 
MELeBlancCommented:
Forgot to add... Since I had to undo all of the permissions settings before.. I had to redo them again on my test server.  This time I made the required IIS change and then just gave the IUSR account full control over that directory... worked like a charm.

-M
0
 
peetmAuthor Commented:
Added this:

   Dim strOutPath
   strOutPath = Server.MapPath(Request.ServerVariables("PATH_INFO"))
   strOutPath = Left(strOutPath,InStrRev(strOutPath,"\")-1)
   ado_stream.SaveToFile strOutPath & "/temp.gif",2  ' 2=adSaveCreateOverWrite

And it worked on my ISP's server!! w00t

Thanks very much!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 10
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now