简体   繁体   English

在Silverlight和异步回调中保存文件

[英]Saving Files in silverlight and asynchronous call backs

In Silverlight you have to save a file using the save file dialog. 在Silverlight中,您必须使用“保存文件”对话框来保存文件。

You can only open this dialog from a user event aka a button click 您只能通过用户事件(也就是单击按钮)打开此对话框

I'm returning the data of a file from a web service call asynchronously 我正在从Web服务调用异步返回文件的数据

How do i save this to file? 如何将其保存到文件?

If i ask them before the service call i can't use the stream after the data comes back. 如果我在服务呼叫之前询问他们,则数据返回后我将无法使用该流。

If i ask them after i can't open the save file dialogue. 如果在无法打开保存文件对话框后问他们。

It's a bit of chicken and the egg situation. 这有点鸡和鸡蛋的情况。

Thanks. 谢谢。

update 更新

i want to be able to save the users computer where they spectifiy not the silverlight isolated storage. 我希望能够将用户计算机保存在他们未指定Silverlight孤立存储的位置。

Open a SaveFileDialog from a user event, then keep a reference to this dialog around. 从用户事件中打开一个SaveFileDialog ,然后保留对该对话框的引用。 Make your web service call, then in the handler for this call, call the OpenFile() method on the SaveFileDialog . 进行Web服务调用,然后在此调用的处理程序中,调用SaveFileDialog上的OpenFile()方法 Use the stream returned by this method to write to your file. 使用此方法返回的流来写入文件。

private SaveFileDialog _mySaveDialog;

private void Button_Click(object sender, EventArgs e)
{
   _mySaveDialog = new SaveFileDialog();
   // Configure the dialog and show it here...
}

// call this method from the handler for your web service call
private void Save(string toSave) 
{
   Stream fileStream = _mySaveDialog.OpenFile();
   // Write to the file here...
}

If you'd like an even more detailed example that uses this same technique, see here . 如果您想要使用该技术的更详细的示例,请参见此处

if i get your question correct, here is proposed solution. 如果我对您的问题正确无误,请提出以下解决方案。

off course there would be a button which will open the save button. 当然,会有一个按钮将打开保存按钮。 but that button would be enabled when the service call completes. 但是该按钮将在服务呼叫完成后启用。 you can save that data temporary into isolated storage. 您可以将该数据临时保存到隔离的存储中。 Enable the button now. 现在启用按钮。 on clicking that open the save diag box, 点击打开保存诊断框后,

here is the code to save data into isolated storage temporary. 这是将数据保存到临时存储临时中的代码。

public static void SaveLog(string data)
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName, FileMode.Append, FileAccess.Write, isf))
                {
                    using (StreamWriter sw = new StreamWriter(isfs))
                    {
                        try
                        {
                            sw.Write(data);

                        }
                        finally
                        {
                            sw.Close();
                        }
                    }
                }
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM