【问题标题】:download file from absolute uri to stream to SaveFileDialog从绝对 uri 下载文件以流式传输到 SaveFileDialog
【发布时间】:2023-04-02 07:00:01
【问题描述】:

我已经将文件从 url 放入流中。 但是,在事件 OpenReadCompleted 中放置 savefiledialog 会给出异常,因为需要从用户启动的事件中触发 savefiledialog。 将 savefiledialog NOT 放在 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());
        }

}

【问题讨论】:

    标签: silverlight silverlight-4.0 uri absolute savefiledialog


    【解决方案1】:

    采取的方法是首先打开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,您并没有浪费他们的时间或带宽下载文件。

    【讨论】:

    • 完美运行!我怎么没想到。可能因为我是新手。非常感谢。
    【解决方案2】:

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

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

    【讨论】:

    • 这个比较适合作为评论。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 1970-01-01
    相关资源
    最近更新 更多