Ho ottenuto fino a mettere un file in un flusso da un URL. Tuttavia, puttin savefiledialog all'interno dell'evento OpenReadCompleted fornisce un'eccezione perché il savefiledialog deve essere attivato da un evento incoerente dell'utente. L'inserimento di savefiledialog NON in OpenReadCompleted restituisce un errore perché l'array di byte è vuoto, non ancora elaborato. C'è un altro modo per salvare un file per lo streaming da un uri senza usare un evento?Scarica il file da assoluto uri a flusso su SaveFileDialog
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());
}
}
Funziona perfettamente! Perché non ci ho pensato. Probabilmente perché sono un principiante. Molte grazie. – tutu