Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass C# byte array to javascript

i have a situation in which i have a byte array of a image in code behind C# class of a webpage (pop up page)

protected void ToFile(byte[] byteImage)

{
            string strByte = byteImage.ToString();
            this.Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup('" + byteImage + "');</script>");
            this.Context.Response.End();
}

i want to get pass byteImage to the handler function i.e .in javascript / on parent page

function onDialogClose(dialogResult,returnValue) {
        if (dialogResult == SP.UI.DialogResult.OK) {
            //var inputBuffer = new System.Byte(returnValue.length);
            //var byte = new Array();
            //byte = returnValue;

how to get the byte array at returnValue (now it contains System.Byte[]) only

is there any way to access C3 byte[] array from Javascript??

thankx

like image 795
Ishaan Puniani Avatar asked Feb 28 '11 10:02

Ishaan Puniani


People also ask

What is pass equivalent in C?

You don't have pass or equivalent keyword. But you can write equivalent code without any such keyword. def f(): pass. becomes void f() {} and class C: pass.

Can we use pass in C?

There are two ways to pass parameters in C: Pass by Value, Pass by Reference.

Do we have pass in C++?

Pass by reference is something that C++ developers use to allow a function to modify a variable without having to create a copy of it. To pass a variable by reference, we have to declare function parameters as references and not normal variables.

What is a pass in coding?

The pass statement is used as a placeholder for future code. When the pass statement is executed, nothing happens, but you avoid getting an error when empty code is not allowed.


2 Answers

You could use the base64 encoding to encode the byte array safely:

var result = Convert.ToBase64String(bytes);

Of course, in order to access the original byte values in JavaScript, you’ll have to convert it back on the JavaScript side. There is no built-in function for this in JavaScript, but you can probably grab the decodeBase64 implementation from this website.

like image 137
Timwi Avatar answered Sep 21 '22 15:09

Timwi


You could use this

private string Bytes2String(byte[] bytes){
    return "["+string.Join(",",bytes.Select(b=>b.ToString()))+"]";
}

provided you are using .Net 4.0

like image 41
bottlenecked Avatar answered Sep 20 '22 15:09

bottlenecked