Solved

Send binary data to server

Posted on 2004-04-02
10
774 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
  • 5
  • 5
10 Comments
 
LVL 28

Expert Comment

by:sybe
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 4

Author Comment

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

What is best solution?
0
 
LVL 28

Expert Comment

by:sybe
Comment Utility
>> 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 4

Author Comment

by:sokolovsky
Comment Utility
>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 500 total points
Comment Utility
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
Comment Utility
> 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
Comment Utility
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
Comment Utility
:)
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
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/…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now