【发布时间】: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