पर फ़ाइल डाउनलोड करें, जहां तक मैं एक यूआरएल से एक स्ट्रीम में फ़ाइल डाल रहा हूं। हालांकि ईवेंट के अंदर puttin savefiledialog OpenReadCompleted एक अपवाद देता है क्योंकि savefiledialog को उपयोगकर्ता इनियेटेड ईवेंट से निकाल दिया जाना चाहिए। savefiledialog को डालने के अंदर नहीं OpenReadCompleted एक त्रुटि देता है क्योंकि बाइट्स सरणी खाली है, अभी तक संसाधित नहीं है। क्या किसी ईवेंट का उपयोग किये बिना यूरी से स्ट्रीम करने के लिए फ़ाइल को सहेजने का कोई और तरीका है?पूर्ण यूरी से धारा में फ़ाइल को 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());
}
}
यह पूरी तरह से काम करता है! मैंने इसके बारे में क्यों नहीं सोचा। शायद क्योंकि मैं एक नौसिखिया हूँ। बहुत बहुत धन्यवाद। – tutu