Link to home
Start Free TrialLog in
Avatar of peetm
peetmFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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.

https://www.experts-exchange.com/questions/22889200/Need-to-POST-a-binary-file-and-save-it.html
Avatar of MELeBlanc
MELeBlanc
Flag of United States of America image

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
Actually... I just realized that it may also answer your question over in ASP as well  ;-)

-M
Avatar of peetm

ASKER

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'
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

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


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


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
Avatar of peetm

ASKER

>>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
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
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
Avatar of peetm

ASKER

>>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!
Avatar of peetm

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of MELeBlanc
MELeBlanc
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of peetm

ASKER

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!