How to pass a byte[] from C# to JavaScript

T

thomas_okken

I'm working on a little C# DLL that will let me capture audio from
within Internet Explorer. The idea is that the C# object, once it is
recording, will periodically invoke a JavaScript callback, which will
handle the audio somehow.

The JavaScript callback is a simple function, which I pass to the C#
object during its initialization; I then invoke the JavaScript
callback using

theCB.GetType().InvokeMember("", BindingFlags.InvokeMethod, null,
theCB, new object[]
{arg0, arg1, ...});

My problem is that I can't figure out how to pass a C# byte array to
the JavaScript callback. As long as I pass primitive types,
everything's fine, but when I pass a byte[], the JavaScript code
doesn't seem to recognize the object; "for (i in theArray)" finds no
properties, and any attempt to do things like theArray[0] (indexing)
or theArray.Length results in an Exception.
What is the right way to pass an array from C# to JavaScript?

Thanks,

- Thomas
 
N

Nicholas Paldino [.NET/C# MVP]

Thomas,

I'm not sure exactly how to do this, but since you already have an
object embedded in IE, why not create a test method which you pass a byte
array from Javascript into the C# object? Then, you can set a breakpoint
(or have some other sort of trace in there) and look at what is passed in.
That should tell you what you need to know in order to return the array back
to Javascript.
 
M

Mr. Arnold

If I remember, you can't do that with JavaScript, because JavaScript only
works with Variant types internally. It's been awhile, but I have returned
System.Arraylist of objects and was able to walk the ArrayList of objects
and access their accessor properties returned to a JavaScript Variant
object. I have done that and also have returned a dataset back to Javascript
and a assign it to a Variant object and walk the dataset.

However, I was doing this using AjaxPro for .Net, which is free to use.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top