I am working on a Silverlight 3 app with C#. I would like to allow the user to download an image from the Silverlight app. I am using SaveFileDialog to perform the file download task. The flow goes this way:
- User clicks on Download button in the SL app.
- Web service call invoked to get the image from server
- OnCompleted async event handler of the web method call get invoked and receives the binary image from the server
- Within the OnCompleted event handler, SaveFileDialog prompted to user for saving the image to computer.
- Stream the image to the file on user's harddrive.
I am using the following code in a function which is called from the OnCompleted event handler to accomplish SaveFileDialog prompt and then streaming to file.
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "JPG Files|*.jpg" + "|All Files|*.*";
bool? dialogResult = dialog.ShowDialog();
if (dialogResult == true)
{
using (Stream fs = (Stream)dialog.OpenFile())
{
fs.Write(e.Result, 0, e.Result.Length);
fs.Close();
}
}
The SaveFileDialog would throw the error "Dialogs must be user-initiated." when invoking ShowDialog method in the above code. What could i be missing here? How to overcome this?