views:

149

answers:

1

I'm trying to call a function in a Silverlight 4 component to open a file dialog and upload a file to the server. Everything works well within Silverlight but I'd like to call the component from JavaScript. So I call the button click handler from a C#-function which is [ScriptableMember]. I get the following error when calling the function:

System.Security.SecurityException: Dialogs must be user-initiated. 
at System.Windows.Browser.ManagedObjectInfo.Invoke(ManagedObject obj, InvokeType
invokeType, String memberName, ScriptParam[] args) at
System.Windows.Hosting.ManagedHost.InvokeScriptableMember(IntPtr pHandle, Int32 nMemberID,
Int32 nInvokeType, Int32 nArgCount, ScriptParam[] pArgs, ScriptParam& pResult,
ExceptionInfo& pExcepInfo)

I based my code on the tutorial by Tim Heuer that can be found here: http://www.silverlight.net/learn/videos/silverlight-videos/openfiledialog-and-file-upload/

I understand why the error is thrown, sort of, the 'click' from the user is done in JavaScript and probably is not going through to Silverlight. For Silverlight this is merely a function call.

Is this new policy from Silverlight 4? Is it still possible to only use Silverlight behind to screens to upload a file? Any help is appreciated.

+2  A: 

Hi, I believe in Silverlight 2 this may have been permitted, but ever since it has not. This was a breaking behavior change, requiring the action to be "user initiated" through a user action -in- Silverlight: something like a key press or mouse click on a button with a click event handler.

There isn't a way to pop up and open the file dialog any other way.

Jeff Wilcox
Thanks for the clarification Jeff, although I am a bit disappointed :)
PeterEysermans
Though disappointing, the situation through JS could be as bad as popup windows: imagine the annoyance of a site that for some reason or another pops up the open file dialog when you browse to it. It blocks the browser and waits for something to happen - even worse if it happens in a loop.
Jeff Wilcox