2012-05-27 92 views
0

我想強制我的程序在做某事之後等待一段時間,然後在asp.net和silverlight中做其他事情。 (具體來說,我想通過silverlight程序上傳圖片,然後通過Image控件將其顯示在我的頁面中,但是當我上傳尺寸大約爲6KB或更大的圖片時,圖片不顯示,但是已經成功升級了。我認爲等待一些時刻可能會解決問題) 任何人都可以引導我嗎? 謝謝在asp.net中顯示wait()(顯示上傳的圖像)

+0

在客戶端等待或在服務器端。我認爲延遲不會解決問題。你的問題必須與你已經忘記的東西的同步有關。 – Aristos

+0

在localhost上運行時沒有任何問題,我認爲上傳並不需要時間。 –

+0

這聽起來像某種競爭條件。如果你發佈你的代碼會有幫助。 – tzerb

回答

0

我使用的代碼this page

這是MyPage.Xaml代碼:

private void UploadBtn_Click(object sender, RoutedEventArgs e) 
    { 

     OpenFileDialog dlg = new OpenFileDialog(); 

     dlg.Multiselect = false; 
     dlg.Filter = UPLOAD_DIALOG_FILTER; 
     if ((bool)dlg.ShowDialog()) 
     { 
      progressBar.Visibility = Visibility.Visible; 
      progressBar.Value = 0; 
      _fileName = dlg.File.Name; 
      UploadFile(_fileName, dlg.File.OpenRead()); 

     } 

     else 
     { 

      //user clicked cancel 

     } 

    } 


    private void UploadFile(string fileName, Stream data) 
    { 
     // Just kept here 
     progressBar.Maximum = data.Length; 

     UriBuilder ub = new UriBuilder("http://mysite.com/receiver.ashx"); 

     ub.Query = string.Format("fileName={0}", fileName); 
     WebClient c = new WebClient(); 

     c.OpenWriteCompleted += (sender, e) => 
     { 

      PushData(data, e.Result); 

      e.Result.Close(); 

      data.Close(); 

     }; 

     try 
     { 
      c.OpenWriteAsync(ub.Uri); 
     } 
     catch (Exception e) 
     { 
      MessageBox.Show(e.ToString()); 
     } 

    } 
    private void PushData(Stream input, Stream output) 
    { 
     byte[] buffer = new byte[input.Length]; 

     int bytesRead; 
     while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0) 
     { 
      output.Write(buffer, 0, buffer.Length); 
     } 
     progressBar.Value += input.Length; 

     if (progressBar.Value >= progressBar.Maximum) 
     { 
      progressBar.Visibility = System.Windows.Visibility.Collapsed; 
      loadImage(); 
     } 
    } 
private void loadImage() 
    { 
     Uri ur = new Uri("http://mysite.com/upload/" + _fileName); 
     img1.Source = new BitmapImage(ur); 
    } 

這是receiver.ashx:

public void ProcessRequest(HttpContext context) 
{ 
    string filename = context.Request.QueryString["filename"].ToString(); using (FileStream fs = File.Create(context.Server.MapPath("~/upload/" + filename))) 
    { 

     SaveFile(context.Request.InputStream, fs); 

    } 

} 
private void SaveFile(Stream stream, FileStream fs) 
{ 

    byte[] buffer = new byte[stream.Length]; 

    int bytesRead; 
    while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0) 
    { 

     fs.Write(buffer, 0, bytesRead); 

    } 

}