Solved

Send binary data to server

Posted on 2004-04-02
10
782 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
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 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