简体   繁体   English

异步实现文件上传控制时,UpdateProgress和触发器部分在“更新”面板中不起作用

[英]UpdateProgress and trigger section not working in Update panel while implement file upload control asynchronously

I am trying to upload image files with asp.net file upload control inside an update panel. 我正在尝试使用更新面板中的asp.net文件上载控件上载图像文件。 I want to process UpdateProgress to show a progress bar image indicating a progress as well along with the file upload. 我想处理UpdateProgress以显示进度条图像,该进度条图像指示进度以及文件上传。

Case 1: When I remove the trigger section and use UpdateProgress section, the progress procedure is working fine but the file upload control fails to upload my files. 情况1:当我删除触发器部分并使用UpdateProgress部分时,进度过程运行正常,但文件上传控件无法上传我的文件。 [ The page doesnot reload ] [ 页面未重新加载 ]

Case 2: When I use trigger section and remove UpdateProgress section, the file gets uploaded but the page gets reloaded. 情况2:当我使用触发器部分并删除UpdateProgress部分时,文件被上传,但页面被重新加载。

Expected: What I really want is a fine file upload process that includes UpdateProgress to show progress image and strictly with no page loads. 预期:我真正想要的是一个很好的文件上传过程,该过程包括UpdateProgress以显示进度图像,并且严格没有页面加载。

What I have been to is: 我去过的是:

.aspx section .aspx部分

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>


<asp:UpdateProgress ID="loading" runat="server">

<ProgressTemplate>

<asp:Image ID="Image1" CssClass="loadingGeneral" ImageUrl="../Images/loading(1).gif" AlternateText="Processing" runat="server" />

</ProgressTemplate>

  </asp:UpdateProgress>

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>

    <asp:FileUpload ID="fileUploadForAlbum" multiple="true" CssClass="buttonclass"  runat="server"  ToolTip="Click to browse image." />
    <asp:Button ID="btn_uploadAlbum" runat="server" class="buttonclass" OnClick="btnUploadAlbum_Click" Text="Upload Slider" />

    </ContentTemplate>
        <Triggers>
             <asp:PostBackTrigger ControlID="btn_uploadAlbum" />
        </Triggers>
        </asp:UpdatePanel>

.aspx.cs section .aspx.cs部分

File upload code section is fine and I have used following to implement progress bar. 文件上传代码部分很好,我已经使用下面的方法来实现进度条。

   protected void btnUploadAlbum_Click(object sender, EventArgs e)
{
            System.Threading.Thread.Sleep(2000);
            do something.........
}

Thanks in advance. 提前致谢。 Any help is appreciated. 任何帮助表示赞赏。

There are some interesting facts to know about FileUpload control. 关于FileUpload控件,有一些有趣的事实要知道。

1.) file upload control doesn't work with asynchronous postback. 1.)文件上传控件不适用于异步回发。 It always needs a postback to work properly. 它总是需要回发才能正常工作。 This is the reason why you see full page postbacks. 这就是您看到整页回发的原因。

2.) AsyncPostbackTrigger will not help here to prevent postback. 2.)AsyncPostbackTrigger在这里无法防止回发。

MSDN says clearly that: MSDN明确指出:

The FileUpload control is designed to be used only in postback scenarios 
and not in asynchronous postback scenarios during partial-page rendering.
When you use a FileUpload control inside an UpdatePanel control, the file must
be uploaded by using a control that is a PostBackTrigger object for the panel

This makes to infer our third point: 这使得我们可以推断出第三点:

3.) We need to use PostBackTrigger to make the FileUpload control work with UpdatePanel , which is then going to have a full page postback. 3.)我们需要使用PostBackTrigger来使FileUpload控件与UpdatePanel ,然后将进行整页的回发。

Your Question : 您的问题

What I really want is a fine file upload process that includes UpdateProgress 
to show progress image and strictly with no page loads

One of the good answer to this is to use the AsyncFileUpload control. 对此的好答案之一是使用AsyncFileUpload控件。 Features of this control: 此控件的功能:

  • It works within the Update Panel 它在更新面板中起作用
  • You can show the loading image while file uploading is in progress, using ThrobberID property. 您可以使用ThrobberID属性在文件上传过程中显示加载图像。
  • It uploads the file without any postback 它上传文件而没有任何回发
  • It provides Client Side and Server side events 它提供客户端和服务器端事件
  • There are different coloring options for showing file upload. 有多种颜色选项可用于显示文件上传。 As for example, it shows green color if upload is successful: Use the CompleteBackColor property, otherwise it shows red if there is unsuccessful upload, using ErrorBackColor property. 例如,如果上传成功,则显示绿色:使用CompleteBackColor属性,如果上传失败,则使用ErrorBackColor属性显示红色。

On your .aspx page, place a script manager and register the Ajax control toolkit DLL. .aspx页面上,放置一个脚本管理器并注册Ajax控件工具包DLL。

<%@ Register Assembly="AjaxControlToolkit" 
    Namespace="AjaxControlToolkit" TagPrefix="ajaxasyncFU" %>

Now place the AsyncFileUpload control: 现在放置AsyncFileUpload控件:

<ajaxasyncFU:AsyncFileUpload ID="AsyncFileUpload1" runat="server" 
OnClientUploadError="uploadError" OnClientUploadStarted="StartUpload" 
OnClientUploadComplete="UploadComplete" 
CompleteBackColor="Lime" UploaderStyle="Modern" 
ErrorBackColor="Red" ThrobberID="Throbber" 
onuploadedcomplete="AsyncFileUpload1_UploadedComplete" 
UploadingBackColor="#66CCFF" />

There are 3 client events you can use: OnClientUploadError , OnClientUploadStarted , and OnClientUploadComplete 您可以使用3个客户端事件: OnClientUploadErrorOnClientUploadStartedOnClientUploadComplete

And one Server side event: onuploadedcomplete , which is called in asynchronous manner thus avoiding full page postback. 还有一个服务器端事件: onuploadedcomplete ,以异步方式调用,因此避免了整个页面的回发。 In this server event usually we save the file: 通常在此服务器事件中,我们保存文件:

protected void AsyncFileUpload1_UploadedComplete
    (object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{      
  if (AsyncFileUpload1.HasFile)
  {
    string strPath = MapPath("~/MyImages/") + Path.GetFileName(e.filename);
    AsyncFileUpload1.SaveAs(strPath);
  }
}

Check these 2 links: Link1 , Link2 for further reading. 检查以下2个链接: Link1Link2以进一步阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM