2011-11-22 25 views
5

Tôi đã nhận được quá xa khi đưa tệp vào luồng từ url. Tuy nhiên puttin savefiledialog bên trong sự kiện OpenReadCompleted đưa ra một ngoại lệ vì savefiledialog cần phải được kích hoạt từ một sự kiện iniated của người dùng. Đặt savefiledialog NOT bên trong OpenReadCompleted đưa ra một lỗi vì mảng byte trống, chưa được xử lý. Có cách nào khác để lưu tệp để phát trực tiếp từ một thiết bị không cần sử dụng sự kiện không?tải xuống tệp từ uri tuyệt đối để phát trực tuyến tới 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()); 
     } 

} 

Trả lời

7

Cách tiếp cận để có được để đầu tiên mở SaveFileDialog như là kết quả của một số tương tác người dùng như một nút bấm. Đã yêu cầu người dùng xác định nơi lưu bản tải xuống và phương thức SaveDialog đã trả về, bạn giữ cho trường hợp đó là SaveFileDialog trên tay.

Sau đó, bạn gọi tải xuống và trong OpenReadCompleted bạn có thể sử dụng phương thức SaveFileDialogOpenFile để nhận luồng mà bạn có thể bơm kết quả.

public void SaveAs() 
{ 
    SaveFileDialog dialog = new SaveFileDialog(); 
    dialog.Filter = "All Files|*.*"; 

    bool? dialogResult = dialog.ShowDialog(); 

    if (dialogResult != true) return; 

    WebClient webClient = new WebClient(); 
    webClient.OpenReadCompleted += (s, e) => 
    { 
     try 
     {  
      using (Stream fs = (Stream)dialog.OpenFile()) 
      { 
       e.Result.CopyTo(fs); 
       fs.Flush(); 
       fs.Close(); 
      } 

     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.ToString()); 
     } 
    }; 
    webClient.OpenReadAsync(new Uri("http://testurl/test.docx"), UriKind.Absolute); 
} 

Bạn sẽ không chỉ là mã sạch hơn và đơn giản hơn mà nếu người dùng kết thúc hủy SaveFileDialog bạn không lãng phí thời gian hoặc băng thông tải xuống tệp.

+0

Nó hoạt động hoàn hảo! Tại sao tôi không nghĩ về điều đó. Có lẽ vì tôi là một người mới. Cảm ơn rất nhiều. – tutu

0

tôi đã tìm thấy cách đơn giản để tải xuống tệp từ ứng dụng Silverlight. sử dụng điều khiển HyperLinkButton.

bạn có thể chỉ định đích cũng sử dụng "TargetName".

+1

Điều này phù hợp hơn làm nhận xét. –

Các vấn đề liên quan