将 C# 字节数组传递给 javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5141006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pass C# byte array to javascript
提问by Ishaan Puniani
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 i.e .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
谢谢
回答by Timwi
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. There is no built-in function for this in JavaScript, but you can probably grab the decodeBase64 implementation from this website.
当然,为了在 JavaScript 中访问原始字节值,您必须在 JavaScript 端将其转换回。JavaScript 中没有为此提供内置函数,但您可能可以从该网站获取 decodeBase64 实现。
回答by bottlenecked
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

