I'm trying to create a Silverlight application that downloads a file accessed by a URL. I tried using WCF, but now I'm attempting to do it with Webclient. When I try using this sample code, I'm getting a securityException error that says "Dialogs must be user initiated." Can someone please explain what I'm doing wrong? Thanks.
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication1
{
public partial class MainPage : UserControl
{
#region Constructors
public MainPage()
{
InitializeComponent();
// SaveFileDialog sfd = new SaveFileDialog();
}
#endregion
#region Handlers
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
if ((bool)sfd.ShowDialog())
{
StreamReader sr = new StreamReader(e.Result);
string str = sr.ReadToEnd();
StreamWriter sw = new StreamWriter(sfd.OpenFile());
sw.Write(str);
}
}
private void download_Click_1(object sender, RoutedEventArgs e)
{
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.OpenReadAsync(new Uri("http://www.productimageswebsite.com/images/stock_jpgs/34891.jpg", UriKind.Absolute));
}
#endregion
}
}