2016-06-01 17 views
0

ファイルアップロードファイル転送中にラベルを更新しようとして問題が発生しています。私は本質的に、ファイル転送のステータスについて実行中のタブを維持しようとしています。しかし、何らかの理由で、ファイル転送を行うためにバックグラウンドワーカーを開始するという関数内で、ラベルを更新することはできません。ファイル転送の前にelse文を入力する前に、Label.Textを変更できます(これは、目的のディレクトリにファイルが重複していることを示しています)。ファイル転送中にラベルテキストが更新されないASP.NET

私は、ラベルをUpdatePanelに置き、UpdateMode = "条件付き"を設定し、手動でUpdatePanel1.Update()を呼び出すという従来の方法は動作しませんでした。

その他の質問でも、ページ内に誤ったjavascriptが表示されるという問題が解決されましたが、このような状況ではそうではありません。このウェブページにはJavaScriptがありません。

また、FileUploadのバックグラウンドワーカーを起動した後で、uiバックグラウンドワーカーとSaveFile()メソッドで実行されたループを通してLabel.Textを設定しようとしました。どちらもうまくいかなかった。

また、私はLabel.Textの内容がメディアから割り当てられたときに更新されることに気付きましたが、ファイル転送が完了するまでクライアント側のUIは更新されません。ラベルをつぶすここで

です。ここHTMLスニペット

<form id="form1" runat="server"> 
     <!-- Here's all of the contents for the asp part of the page --> 
     <br /> 
     <h1>Upload File</h1> 
     <asp:ScriptManager ID="ScriptMgr" runat="server"></asp:ScriptManager> 
     <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true"> 
     <ContentTemplate> 
      <p> 
       <asp:Label ID="UploadLabel" runat="server"></asp:Label> 
      </p> 
     </ContentTemplate> 
     </asp:UpdatePanel> 
     <asp:FileUpload ID="UploadFile" runat="server"/> 
     <br /> 
     <!-- OnClick="BtnUpload_Click" --> 
     <asp:Button ID="BtnUpload" runat="server" Text="Upload File" OnClick="BtnUpload_Click" /> 
     <br /> 
     <br /> 
     <asp:Label ID="RebootLabel" runat="server" Text=""></asp:Label> 
     <br /> 
     <asp:Button ID="BtnReboot" runat="server" Text="Reboot" OnClick="BtnReboot_Click" /> 

</form> 

ある

protected void SaveFile(HttpPostedFile file) 
{ 
    try 
    { 
     String savePath = Resources.Resource.INSTALLER_PATH + UploadFile.FileName;   //save path on the server 
     //check to see if there are any duplicate file names in the destination 
     if (System.IO.File.Exists(savePath)) 
     { 
      //then the file already exists and we should notify the user 
      //do not write anything to the directory if this occurs 
      UploadLabel.Text = "A file with the desired name already exists in the destination directory, please choose another file"; 
     } 
     else 
     { 
      //then it is safe to upload the file to the TOD 
      /*UploadLabel.Text = "Uploading file..."; 
      BtnReboot.Enabled = false; 
      System.Drawing.Color temp = BtnReboot.BackColor; 
      BtnReboot.BackColor = System.Drawing.Color.Black; 
      UploadFile.SaveAs(savePath); //upload the file to the TOD 
      BtnReboot.BackColor = temp; 
      BtnReboot.Enabled = true; 
      UploadLabel.Text = "Finished uploading file.";*/ 
      try 
      { 
       UploadLabel.Text = "Uploading file..."; 
       uploadingFileName = savePath;  //get the path that is being uploaded to 
       uploadingFileSize = UploadFile.PostedFile.ContentLength;  //get the size in bytes to upload 
       BackgroundWorker bgw = new BackgroundWorker(); 
       bgw.DoWork += Bgw_DoWork; 
       bgw.RunWorkerAsync(); 
       //progress report ui worker 
       BackgroundWorker uiWorker = new BackgroundWorker(); 
       uiWorker.DoWork += UiWorker_DoWork; 
       uiWorker.RunWorkerAsync(); 
       bgw.Dispose(); 
       uiWorker.Dispose(); 
      } 
      catch (Exception err) 
      { 
       UploadLabel.Text = err.ToString(); 
      } 
     } 
    } 
    catch (System.Web.HttpException err) 
    { 
     UploadLabel.Text = "Exception: " + err.ToString(); 
    } 
    catch (System.InvalidOperationException err) 
    { 
     UploadLabel.Text = "Exception: " + err.ToString(); 
    } 
    catch (System.UriFormatException err) 
    { 
     UploadLabel.Text = "Exception: " + err.ToString(); 
    } 
} 

private void UiWorker_DoWork(object sender, DoWorkEventArgs e) 
{ 
    while(uploadingFileSize != 0) 
    { 
     //redraw the label 
     if (File.Exists(uploadingFileName)) 
     { 
      FileInfo fi = new FileInfo(uploadingFileName); 
      long currentSize = fi.Length; 
      UploadLabel.Text = "Progress: " + currentSize + "/" + uploadingFileSize; 
      UpdatePanel1.Update(); 
     } 
    } 
} 

/// <summary> 
/// Bgw_DoWork 
/// Asynchronous function that gets called for the background worker to start work 
/// Is used for file uploading. Combined with the timer to give feedback on current upload progress 
/// </summary> 
/// <param name="sender"></param> 
/// <param name="e"></param> 
private void Bgw_DoWork(object sender, DoWorkEventArgs e) 
{ 
    UploadLabel.Text = "Hello from the bgw"; 
    UploadFile.SaveAs(uploadingFileName); 
    uploadingFileSize = 0; 
    uploadingFileName = ""; 
    //BackgroundWorker worker = sender as BackgroundWorker; 
    //worker.ReportProgress(0); 
} 

答えて

0

主な問題は、あなたが両方のアクションを(ファイルをアップロードし、ラベルを更新)やりたいということです関連する.cs方法同じ要求でファイルをアップロードすると、サーバーにバイトストリームを送信しており、応答を待つ必要があり、ファイルが完全にアップロードされた後に応答が返されることを考慮してください。だから、あなたがする必要があるのは、2つのajaxリクエストをjquery($ .ajax()メソッド)またはXMLHttpRequestをスローすることです。更新パネルを使用することは、あなたがしていることのためのオプションではありません。

は、あなたがそれを行うことができますどのように表示するには、このに見てみましょう:http://abandon.ie/notebook/simple-file-uploads-using-jquery-ajax

+0

これに伴う問題は、サーバーにファイルをアップロードするために、AJAXでPHPのいくつかの種類を使用する必要があることです。アップロード時にファイルサイズの制限を変更できるため、ASP.netを使用していました。 PHPではできませんが、これは問題を引き起こしました。少なくともそれは私が読んできたもので、最近発見したものです。私は間違っている可能性がある。 – John

関連する問題