?
Solved

Send binary data to server

Posted on 2004-04-02
10
Medium Priority
?
788 Views
Last Modified: 2008-03-03
Some ActiveX returns me array of bytes on client side.
I have to send this array of bytes to server.
How?

Now i'm using such scheme:

On Client:

<FORM ID="frm" Name="frm" ACTION="Form2.asp" METHOD="post">
            <INPUT TYPE="HIDDEN" NAME="txtAction" ID="txtAction">
            <INPUT TYPE="Button" ID="btnSendMessage" NAME="btnSendMessage" VALUE="Send" OnClick="frm.submit();">
</FORM>
 <SCRIPT LANGUAGE="VBScript">
   <!--
      Dim zz(99999) 'Just example
      Dim strZ
      for i=0 to 99999
            zz(i) = i mod 256
      Next
      strZ=Join(zz,"/")
      document.all("txtAction").Value = strZ
   //-->
   </SCRIPT>

On Server side (Form2.asp):

<%@ Language=VBScript LCID=1033 CODEPAGE=65001%>
<%
Dim m_strAction
Dim FormFields

Function BinaryToString(Binary)
      Dim RS, LBinary
      Const adLongVarChar = 201
      Set RS = CreateObject("ADODB.Recordset")
      LBinary = LenB(Binary)
      If LBinary>0 Then
            RS.Fields.Append "mBinary", adLongVarChar, LBinary
            RS.Open
            RS.AddNew
              RS("mBinary").AppendChunk Binary
            RS.Update
            BinaryToString = RS("mBinary")
      Else
            BinaryToString = ""
      End If
End Function

Function RSBinaryToString(xBinary)
      Dim Binary
      If vartype(xBinary)=8 Then Binary = MultiByteToBinary(xBinary) Else Binary = xBinary
      Dim RS, LBinary
      Const adLongVarChar = 201
      Set RS = CreateObject("ADODB.Recordset")
      LBinary = LenB(Binary)
      If LBinary>0 Then
            RS.Fields.Append "mBinary", adLongVarChar, LBinary
            RS.Open
            RS.AddNew
              RS("mBinary").AppendChunk Binary
            RS.Update
            RSBinaryToString = RS("mBinary")
      Else
            RSBinaryToString = ""
      End If
End Function

Function MultiByteToBinary(MultiByte)
      Dim RS, LMultiByte, Binary
      Const adLongVarBinary = 205
      Set RS = CreateObject("ADODB.Recordset")
      LMultiByte = LenB(MultiByte)
      If LMultiByte>0 Then
            RS.Fields.Append "mBinary", adLongVarBinary, LMultiByte
            RS.Open
            RS.AddNew
              RS("mBinary").AppendChunk MultiByte & ChrB(0)
            RS.Update
            Binary = RS("mBinary").GetChunk(LMultiByte)
      End If
      MultiByteToBinary = Binary
End Function

Function GetForm
      Dim FormFields 'Dictionary which will store source fields.
      Set FormFields = CreateObject("Scripting.Dictionary")
      'If there are some POST source data
      If Request.Totalbytes>0 And Request.ServerVariables("HTTP_CONTENT_TYPE") = "application/x-www-form-urlencoded" Then    
            Dim SourceData
            SourceData = Request.BinaryRead(Request.Totalbytes) 'Read the data
            SourceData = BinaryToString(SourceData) 'Convert source binary data To a string
            SourceData = Split(SourceData, "&") 'Form fields are separated by "&"
            Dim Field, FieldName, FieldContents
            For Each Field In SourceData
                  Field = split(Field, "=") 'Field name And contents is separated by "="
                  FieldName = UnEscape(Field(0))
                  FieldContents = UnEscape(Field(1))
                  FormFields.Add FieldName, FieldContents 'Add field To the dictionary
            Next
      End If 'Request.Totalbytes>0
      Set GetForm = FormFields
End Function

Function GetStringFromArr(ByteArray)
      Dim i
      Const adTypeBinary = 1
      Const adTypeText = 2
      Dim BinaryStream
      Set BinaryStream = CreateObject("ADODB.Stream")
      'BinaryStream.Type = adTypeBinary
      BinaryStream.Type = adTypeText
      BinaryStream.Open
      For i = LBound(ByteArray) To UBound(ByteArray)
            BinaryStream.WriteText ChrB(CByte(ByteArray(i)))
      Next
      BinaryStream.Position = 0
      BinaryStream.Type = adTypeBinary
      BinaryStream.Position = 2
      GetStringFromArr = BinaryStream.Read
      Set BinaryStream = Nothing
End Function

Function SaveBinaryData(FileName, ByteArray)
      Const adTypeBinary = 1
      Const adSaveCreateOverWrite = 2
      Dim BinaryStream
      Set BinaryStream = CreateObject("ADODB.Stream")
      BinaryStream.Type = adTypeBinary
      BinaryStream.Open
      BinaryStream.Write ByteArray
      BinaryStream.SaveToFile FileName, adSaveCreateOverWrite
      Set BinaryStream = Nothing
End Function

      Set FormFields = GetForm
      If Not IsEmpty(FormFields("txtAction")) Then
            m_strAction = FormFields("txtAction")
      End If

      zz=Split(m_strAction,"/")
   m_strAction = GetStringFromArr(zz)
   Call SaveBinaryData("c:\1.bin", m_strAction)
      Response.Write("Ok. " & LenB(m_strAction) & " bytes saved.")
      Response.Flush
      Response.End
%>

Any ideas how to optimize?
0
Comment
Question by:sokolovsky
[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
  • 5
  • 5
10 Comments
 
LVL 28

Expert Comment

by:sybe
ID: 10740425
do you want fileupload?
or what?

please be clear in what you want to reach.
There's many ways to handle binaries. For example use "multipart/form-data" or use base64 encoding.
0
 
LVL 28

Expert Comment

by:sybe
ID: 10740460
the easiest would be to use base64 and XML.

if it is ActiveX, then I suppose it's IE only. That makes it easier.

These really fast functions for base64-encoding and -decoding:

Function ToBase64(ByVal vCode)
    Dim oXML, oNode

    Set oXML = Server.CreateObject("Msxml2.DOMDocument.3.0")
    Set oNode = oXML.CreateElement("base64")
    oNode.dataType = "bin.base64"
    oNode.nodeTypedValue = vCode
    ToBase64 = oNode.text
    Set oNode = Nothing
    Set oXML = Nothing
End Function

Function FromBase64(ByVal vCode)
    Dim oXML, oNode

    Set oXML = Server.CreateObject("Msxml2.DOMDocument.3.0")
    Set oNode = oXML.CreateElement("base64")
    oNode.dataType = "bin.base64"
    oNode.text = vCode
    FromBase64 = oNode.nodeTypedValue
    Set oNode = Nothing
    Set oXML = Nothing
End Function

If you run one of them on the client, you have of course to replace the

Set oObject = Server.CreateObject("")

with

Set oObject = new ActiveX("")
0
 
LVL 4

Author Comment

by:sokolovsky
ID: 10740577
Ok. I have ActiveX, this ActiveH returns me array of bytes(it's wave file contents).
I have to save this wave on server. Without user interaction!

Is it possible to use
Set oXML = new ActiveX("Msxml2.DOMDocument.3.0") on client without installing any additional libraries?
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 4

Author Comment

by:sokolovsky
ID: 10740592
Base64 is nice, but increases size of data to transfer.
My solution too...

What is best solution?
0
 
LVL 28

Expert Comment

by:sybe
ID: 10740652
>> Is it possible to use Set oXML = new ActiveX("Msxml2.DOMDocument.3.0") on client without installing any additional libraries?
Yes, IE 5 and IE 6 come with the XML libraries. Although I am not sure if IE5 comes with a version that supports base64 nodes.

Do I understand it right that you want the user to talk in a microphone and you want a procedure to send it to the server?

>> Base64 is nice, but increases size of data to transfer.
The alternative is to use a form with multipart/form-data. But if you won't let your browser create the multipart/form-data, you have to do it
yourself. hat can be done using VBScript, but you really have to understand how the multipart/form-data protocol works. Anyway, it's much harder and more sensitive to errors then using base64. And you can not use a normal form, you have to post it from the browser by a script, not by form-submission.
0
 
LVL 4

Author Comment

by:sokolovsky
ID: 10741033
>The alternative is to use a form with multipart/form-data.
>But if you won't let your browser create the multipart/form-data, you have to do it yourself.
>That can be done using VBScript, but you really have to understand how the multipart/form-data protocol works.
>Anyway, it's much harder and more sensitive to errors then using base64.
> And you can not use a normal form, you have to post it from the browser by a script, not by form-submission.

I understand how the multipart/form-data protocol works. But how can i SIMULATE fileupload from clientside?
Example, please.

0
 
LVL 28

Accepted Solution

by:
sybe earned 2000 total points
ID: 10741119
use XMLHTTP:

Set oRequest = new ActiveX("Microsoft.XMLHTTP")
oRequest.Open "POST", "http://www.domain.com/script.asp" , False
oRequest.setRequestHeader "Content-Type", "multipart/form-data;" & yourboundary
oRequest.Send (your binary data)
Set oRequest = Nothing


0
 
LVL 4

Author Comment

by:sokolovsky
ID: 10741202
> Set oRequest = new ActiveX("Microsoft.XMLHTTP")
This works fine on clientside? EVERY client?
No troubles with proxies, security?
With what software XMLHTTP library installed?
0
 
LVL 28

Expert Comment

by:sybe
ID: 10741395
XMLHTTP comes with IE 5 and higher i believe. I have used it (without installing it specifically) on Win98 with IE6.
It uses the http protocol (over port 80). Maybe it has some proxy-issues, but i don't know about it. It's just like simulating a browser. You can even add a UserAgent string to the request :)
0
 
LVL 4

Author Comment

by:sokolovsky
ID: 10741540
:)
Sounds great!
I'll try to use this.
Thanks!

By the way, do not forget to post your answer into
http://www.experts-exchange.com/Web/Web_Languages/ASP/Q_20933164.html
to get additional points.
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

752 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