ATL创建的ActiveX实现JS回调
最近公司的产品需要使用ActiveX文件上传。讨论了基本所有的技术,最后还是决定C++搞个ActiveX。。。但上传的回调费了半天劲,才搞定。代码如下
STDMETHODIMP CFileSelector::TestJSInvoke(VARIANT scriptCallback) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); CComPtr<IDispatch> spCallback; if(scriptCallback.vt == VT_DISPATCH) spCallback = scriptCallback.pdispVal; CComVariant avarParams[1]; CFileDialog dlgFile(TRUE,NULL,NULL,OFN_ALLOWMULTISELECT | OFN_FILEMUSTEXIST,L"所有文件(*.*)|*.*||",NULL,NULL,FALSE); if(dlgFile.DoModal() == IDOK) { avarParams[0] = dlgFile.GetPathName(); } DISPPARAMS params = { avarParams, NULL,1,0 }; if(spCallback) spCallback->Invoke(0, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, ¶ms,NULL,NULL,NULL); return S_OK; }
JS调用方法:
<script type="text/javascript"> function OnClick() { var activeX = document.getElementById("ActiveX"); activeX.FileExtFilter = "所有文件(*.*)|*.*||"; activeX.TestJSInvoke(function (e) { alert(e); }); return false; } </script>