Link to home
Start Free TrialLog in
Avatar of sokolovsky
sokolovsky

asked on

Send binary data to server

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?
Avatar of sybe
sybe

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.
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("")
Avatar of sokolovsky

ASKER

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?
Base64 is nice, but increases size of data to transfer.
My solution too...

What is best solution?
>> 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.
>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.

ASKER CERTIFIED SOLUTION
Avatar of sybe
sybe

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
> Set oRequest = new ActiveX("Microsoft.XMLHTTP")
This works fine on clientside? EVERY client?
No troubles with proxies, security?
With what software XMLHTTP library installed?
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 :)
:)
Sounds great!
I'll try to use this.
Thanks!

By the way, do not forget to post your answer into
https://www.experts-exchange.com/questions/20933164/Send-binary-data-from-client-to-server.html
to get additional points.