簡體   English   中英

標簽文本在文件傳輸過程中未更新ASP.NET

[英]Label text not updating during file transfer ASP.NET

在文件上傳文件傳輸期間嘗試更新標簽時遇到麻煩。 我本質上是在嘗試使文件傳輸的狀態保持運行狀態。 但是無論出於什么原因,我都無法在我正在啟動后台工作程序進行文件傳輸的函數中更新標簽。 我可以在文件傳輸之前輸入Label.Text之前更改Label.Text(這是指出所需目錄中文件重復的部分)。

我一直在尋找答案大約兩天,而將Label放入UpdatePanel並設置UpdateMode =“ Conditional”然后手動調用UpdatePanel1.Update()的常規方法不起作用。

其他問題也解決了頁面中存在錯誤的javascript的問題,在這種情況下並非如此。 我在此網頁上沒有任何JavaScript。

我還嘗試通過ui后台工作程序以及在啟動文件上傳的后台工作程序后通過SaveFile()方法中運行的循環來設置Label.Text。 兩者都不起作用。

還要注意的是,我注意到,當我通過任何介質分配Label.Text內容時,Label.Text內容都會更新,但是在文件傳輸完成之前,它不會刷新客戶端的UI,這將進度報告呈現在Label Moot中。

這是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>

這是相關的.cs方法

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);
}

主要問題是您要對同一請求執行兩個操作(上傳文件和更新標簽)。 考慮以下事實:上傳文件后,您將向服務器發送字節流,並且需要等待響應,並且響應將在文件完全上傳之后進行。 因此,您需要做的是觸發2個Ajax請求引發jquery($ .ajax()方法)或XMLHttpRequest。 使用更新面板不是您要做什么的選擇。

看一看,看看如何做到: http : //abandon.ie/notebook/simple-file-uploads-using-jquery-ajax

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM