简体   繁体   English

将C#字节数组传递给javascript

[英]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) 我有一种情况,我在网页的C#类后面的代码中有一个图像的字节数组(弹出页面)

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 ie .in javascript / on parent page 我想将byteImage传递给处理函数,即.in javascript /在父页面上

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 如何在returnValue (现在它只包含System.Byte [])获取字节数组

is there any way to access C3 byte[] array from Javascript?? 有没有办法从Javascript访问C3 byte []数组?

thankx thankx

You could use the base64 encoding to encode the byte array safely: 您可以使用base64编码安全地编码字节数组:

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. 当然,为了在JavaScript中访问原始字节值,您必须在JavaScript端将其转换回来。 There is no built-in function for this in JavaScript, but you can probably grab the decodeBase64 implementation from this website . 在JavaScript中没有内置函数,但你可以从这个网站获取decodeBase64实现

You could use this 你可以用它

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

provided you are using .Net 4.0 如果您使用的是.Net 4.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM