简体   繁体   中英

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. How can I do that?

MIDL:

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

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:

  $("#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') . If you've put a name attribute on that field so that it becomes part of the HTTP submit, that part is good.

As for myActiveX.Data , I imagine that this is some sort of JavaScript object. Remember that only a string can be put into an HTML input; it does not hold binary data.

What I would do is put a breakpoint before $("#hiddenField").val(myActiveX.Data); . Use the debugger keyword if you're not familiar with it. Run the code in your debugger and look at the structure of the value of 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"

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

I'm assuming the C++ code is the server side code.

The best way to handle this is to serialise the 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.

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.
  • Option 2: Use Hex, Base64, etc. to encode the data into a printable format and putting that data into the hidden field.

Either way, when you need to get the data back, just de-serialise it with the matching function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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