【问题标题】:How to save a byte array to a file from silverlight如何从 silverlight 将字节数组保存到文件中
【发布时间】:2010-02-01 15:53:50
【问题描述】:

我有一个连接到 WCF 服务的 SL 3 应用程序。此服务检索字节数组。我想使用 FileStream 将该数组保存为 pdf 文件。问题是,当检索字节数组时,我在尝试显示 SaveFileDialog 时遇到异常,因为该操作是由回调方法启动的,而不是来自用户操作,似乎。 我想知道是否有任何解决方法。我已经有了字节数组,现在我需要将它保存到用户指定的位置。不管怎样... 有什么线索吗?

提前致谢。

【问题讨论】:

    标签: wcf silverlight file silverlight-3.0


    【解决方案1】:

    您是否连接到异步方法调用的方法完成事件?看到这个

    http://www.silverlightshow.net/items/Using-the-SaveFileDialog-in-Silverlight-3.aspx

    在您的回调方法中,您可以实现写入文件的逻辑 - 首先打开对话框,然后获取指向文件流的指针,如下所示。

           try 
           {
               byte[] fileBytes = //your bytes here 
               SaveFileDialog dialog=new SaveFileDialog();
    
               //Show the dialog              
               bool? dialogResult = this.dialog.ShowDialog();  
    
               if (dialogResult!=true) return;
    
    
                //Get the file stream
    
                using ( Stream fs = ( Stream )this.dialog.OpenFile() )  
                {  
                    fs.Write( fileBytes, 0, fileBytes.Length );  
                    fs.Close();  
    
                    //File successfully saved
                }  
            }  
            catch ( Exception ex )  
            {  
                //inspect ex.Message  
            }  
    

    【讨论】:

    • 嗨,amazedsaint,我不知道出了什么问题。我评论了一些代码,然后取消了评论,然后对话框出现了 ¿¿??但在那之后,我在编写字节数组时遇到了一些麻烦,但你在这里的回答是解决方案。非常感谢!!
    猜你喜欢
    • 2021-05-25
    • 1970-01-01
    • 2013-10-19
    • 1970-01-01
    • 1970-01-01
    • 2013-10-27
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多