简体   繁体   English

如何将 SAFEARRAY(字节数组)放入 HTML 隐藏字段

[英]How do I put SAFEARRAY (array of bytes) to HTML Hidden field

I'd like to get array of bytes from active-x component, store that in html-form input hidden field and then pass it to server via form-submit.我想从 active-x 组件中获取字节数组,将其存储在 html-form 输入隐藏字段中,然后通过 form-submit 将其传递给服务器。 How can I do that?我怎样才能做到这一点?

MIDL:中度:

HRESULT Data([out, retval] SAFEARRAY(VARIANT) *pArray);

C++/ATL C++/ATL

STDMETHODIMP MyActiveX::get_Data(SAFEARRAY **pArray)
{
    CComSafeArray<BYTE> arr;    
    for (int i = 0; i < 10; i++)
    {
        CComVariant a;
        a = (BYTE)i;
        arr.Add(a);
    }

    arr.CopyTo(pArray);
    return S_OK;
}

Javascript: Javascript:

  $("#hiddenField").val(myActiveX.Data);

Browser tells me: type mismatch浏览器告诉我:类型不匹配

Although I am not familiar with your exact situation, I have seen some similar situations before.虽然我不熟悉你的具体情况,但我之前也看到过一些类似的情况。

You are correct to put your data in a field using $('#hiddenField') .使用$('#hiddenField')将数据放入字段中是正确的。 If you've put a name attribute on that field so that it becomes part of the HTTP submit, that part is good.如果您在该字段上添加了name属性,使其成为 HTTP 提交的一部分,则该部分很好。

As for myActiveX.Data , I imagine that this is some sort of JavaScript object.至于myActiveX.Data ,我想这是某种 JavaScript object。 Remember that only a string can be put into an HTML input;请记住,只能将字符串放入 HTML 输入; it does not hold binary data.它不保存二进制数据。

What I would do is put a breakpoint before $("#hiddenField").val(myActiveX.Data);我要做的是在$("#hiddenField").val(myActiveX.Data);之前放置一个断点。 . . Use the debugger keyword if you're not familiar with it.如果您不熟悉它,请使用debugger关键字。 Run the code in your debugger and look at the structure of the value of myActiveX.Data .在调试器中运行代码并查看myActiveX.Data值的结构。 It probably has some sort of wrapper field.它可能有某种包装字段。

Alternatively, if you don't have access to a good JavaScript debugger, try the following"或者,如果您无法访问良好的 JavaScript 调试器,请尝试以下“

for(x in myActiveX.Data)
 alert(x + ": " + myActiveX.Data[x]);

I'm assuming the C++ code is the server side code.我假设 C++ 代码是服务器端代码。

The best way to handle this is to serialise the SAFEARRAY.处理此问题的最佳方法是序列化 SAFEARRAY。 From there you can handle it in two ways.从那里您可以通过两种方式处理它。

Firstly, the serialisation.首先,序列化。 I've looked at MSDN and I think using LPSAFEARRAY_Marshal and LPSAFEARRAY_Unmarshal (with an optional IDispatch or IUnknown IID to specify the type, but the documentation doesn't say how it's used) or LPSAFEARRAY_UserMarshal and LPSAFEARRAY_UserUnmarshal to convert the SAFEARRAY to/from a serialised format.我查看了 MSDN,我认为使用LPSAFEARRAY_MarshalLPSAFEARRAY_Unmarshal (使用可选的IDispatchIUnknown IID 来指定类型,但文档没有说明它是如何使用的)或LPSAFEARRAY_UserMarshalLPSAFEARRAY_UserUnmarshal将 SAFEARRAY 转换为序列化格式。

Secondly, handling the data transfer.其次,处理数据传输。

  • Option 1: Save the serialised data on the server side and put a token representing the saved file into the hidden field.方案一:将序列化后的数据保存在服务器端,将代表保存文件的token放入隐藏字段。
  • Option 2: Use Hex, Base64, etc. to encode the data into a printable format and putting that data into the hidden field.选项 2:使用 Hex、Base64 等将数据编码为可打印格式并将该数据放入隐藏字段。

Either way, when you need to get the data back, just de-serialise it with the matching function.无论哪种方式,当您需要取回数据时,只需使用匹配的 function 对其进行反序列化。

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

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