Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Silverlight 将文件从绝对uri下载到流到SaveFileDialog_Silverlight_Silverlight 4.0_Uri_Absolute_Savefiledialog - Fatal编程技术网

Silverlight 将文件从绝对uri下载到流到SaveFileDialog

Silverlight 将文件从绝对uri下载到流到SaveFileDialog,silverlight,silverlight-4.0,uri,absolute,savefiledialog,Silverlight,Silverlight 4.0,Uri,Absolute,Savefiledialog,我已经将文件从url放入流中。 但是,将savefiledialog放入事件OpenReadCompleted中会出现异常,因为savefiledialog需要从用户初始化的事件中激发。 将savefiledialog不放在OpenReadCompleted中会出现错误,因为字节数组为空,尚未处理。 有没有其他方法可以从uri将文件保存到流而不使用事件 public void SaveAs() { WebClient webClient = new WebClient(); //Provides

我已经将文件从url放入流中。 但是,将savefiledialog放入事件OpenReadCompleted中会出现异常,因为savefiledialog需要从用户初始化的事件中激发。 将savefiledialog不放在OpenReadCompleted中会出现错误,因为字节数组为空,尚未处理。 有没有其他方法可以从uri将文件保存到流而不使用事件

public void SaveAs()
{
WebClient webClient = new WebClient(); //Provides common methods for sending data to and receiving data from a resource identified by a URI.
        webClient.OpenReadCompleted += (s, e) =>
                                           {
                                               Stream stream = e.Result; //put the data in a stream
                                               MemoryStream ms = new MemoryStream();
                                               stream.CopyTo(ms);
                                               bytes = ms.ToArray();
                                           };  //Occurs when an asynchronous resource-read operation is completed.
        webClient.OpenReadAsync(new Uri("http://testurl/test.docx"), UriKind.Absolute);  //Returns the data from a resource asynchronously, without blocking the calling thread.

        try
        {
        SaveFileDialog dialog = new SaveFileDialog();
        dialog.Filter = "All Files|*.*";

        //Show the dialog
        bool? dialogResult = dialog.ShowDialog();

        if (dialogResult != true) return;

        //Get the file stream
        using (Stream fs = (Stream)dialog.OpenFile())
        {
            fs.Write(bytes, 0, bytes.Length);
            fs.Close();

            //File successfully saved
        }
        }
        catch (Exception ex)
        {
            //inspect ex.Message
            MessageBox.Show(ex.ToString());
        }

}

要采取的方法是首先打开
SaveFileDialog
,这是一些用户交互的结果,比如单击按钮。让用户确定下载的保存位置,并且
SaveDialog
方法返回后,您可以保留
SaveFileDialog
的实例

然后调用下载,在
OpenReadCompleted
中,您可以使用
SaveFileDialog
OpenFile
方法获取一个流,您可以将结果输出到其中

public void SaveAs() 
{
    SaveFileDialog dialog = new SaveFileDialog(); 
    dialog.Filter = "All Files|*.*"; 

    bool? dialogResult = dialog.ShowDialog(); 

    if (dialogResult != true) return;

    WebClient webClient = new WebClient();
    webClient.OpenReadCompleted += (s, e) => 
    { 
        try 
        {      
            using (Stream fs = (Stream)dialog.OpenFile()) 
            {
                e.Result.CopyTo(fs); 
                fs.Flush(); 
                fs.Close(); 
            }

         } 
         catch (Exception ex) 
         { 
             MessageBox.Show(ex.ToString()); 
         } 
    }; 
    webClient.OpenReadAsync(new Uri("http://testurl/test.docx"), UriKind.Absolute); 
} 

您会注意到,不仅代码更干净、更简单,而且如果用户最终取消了SaveFileDialog,您就不会浪费他们下载文件的时间或带宽。

我找到了从silverlight应用程序下载文件的简单方法。 使用HyperLinkButton控件

也可以使用“TargetName”属性指定目标


它工作得非常好!我怎么没想到呢。可能是因为我是个新手。非常感谢。这更适合作为评论。