【发布时间】:2011-08-05 15:57:51
【问题描述】:
我正在尝试创建一个 Silverlight 应用程序,用于下载通过 URL 访问的文件。我尝试使用 WCF,但现在我尝试使用 Webclient 来实现。 当我尝试使用此示例代码时,我收到一个 securityException 错误,上面写着“必须由用户启动对话框”。 有人可以解释我做错了什么吗?谢谢。
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
}
}
【问题讨论】:
-
我同意这是重复的,但前一个的答案并不令人满意(IMO),而且这个问题有点老了。我会在这里回答。
标签: silverlight webclient