I've gotten as far as putting a file into a stream from a url. However puttin savefiledialog inside the event OpenReadCompleted gives an exception because the savefiledialog needs to be fired from an user iniated event. Putting the savefiledialog NOT inside OpenReadCompleted gives an error because the bytes array is empty, not yet processed. Is there another way to save a file to stream from a uri without using an event?
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());
}
}