[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2007-10-12
16
Medium Priority
?
816 Views
Last Modified: 2012-06-27
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
Comment
Question by:peetm
  • 10
  • 5
15 Comments
 
LVL 6

Expert Comment

by:MELeBlanc
ID: 20064506
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
 
LVL 6

Expert Comment

by:MELeBlanc
ID: 20064512
Actually... I just realized that it may also answer your question over in ASP as well  ;-)

-M
0
 
LVL 10

Author Comment

by:peetm
ID: 20064925
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 6

Expert Comment

by:MELeBlanc
ID: 20065086
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
 
LVL 6

Expert Comment

by:MELeBlanc
ID: 20065711

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
 
LVL 6

Expert Comment

by:MELeBlanc
ID: 20065798


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
 
LVL 6

Expert Comment

by:MELeBlanc
ID: 20065883

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
 
LVL 10

Author Comment

by:peetm
ID: 20065904
>>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
 
LVL 6

Expert Comment

by:MELeBlanc
ID: 20065961
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
 
LVL 6

Expert Comment

by:MELeBlanc
ID: 20065977
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
 
LVL 10

Author Comment

by:peetm
ID: 20066088
>>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
 
LVL 10

Author Comment

by:peetm
ID: 20066166
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
 
LVL 6

Accepted Solution

by:
MELeBlanc earned 1000 total points
ID: 20066462

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
 
LVL 6

Expert Comment

by:MELeBlanc
ID: 20066489
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
 
LVL 10

Author Comment

by:peetm
ID: 20066899
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

Featured Post

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.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month18 days, 19 hours left to enroll

834 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