标签文本在文件传输ASP.NET期间不更新

时间:2016-06-01 16:20:54

标签: c# asp.net

我在尝试在文件上传文件传输期间更新标签时遇到了麻烦。我本质上是试图保持文件传输状态的运行选项卡。无论出于何种原因,我无法在我启动后台工作程序进行文件传输的函数内部更新标签。我可以在文件传输之前进入else语句之前更改Label.Text(这是声明所需目录中存在重复文件的部分)。

我一直在寻找大约两天的答案,并且将Label放入UpdatePanel并设置UpdateMode =“Conditional”然后手动调用UpdatePanel1.Update()的传统方法不起作用。

其他问题也解决了页面中存在错误javascript的问题,在这种情况下情况并非如此。我在这个网页上没有任何javascript。

我还尝试通过ui后台工作器和在fileupload的后台工作程序启动后在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>

以下是相关的.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);
}

1 个答案:

答案 0 :(得分:0)

主要问题是您希望使用相同的请求执行这两个操作(上传文件和更新标签)。考虑一下这样的事实:一旦您上传文件,您就会向服务器发送一个字节流,您需要等待响应,并且响应将在文件完全上传之后。所以你需要做的是触发2个ajax请求抛出jquery($ .ajax()方法)或XMLHttpRequest。使用更新面板不是您要做的事情的选项。

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