asp.net AsyncFileUpload - show list of uploaded fi

2019-09-04 12:55发布

问题:

I use ajaxtoolkit AsyncFileUpload and I want to show list of uploaded files, a last error and handle repeater itemCommand to delete uploaded file.

<asp:AsyncFileUpload ID="uploader1" 
    runat="server" 
    OnUploadedComplete="AsyncFileUpload1_UploadComplete"
    OnClientUploadError="uploadError" 
    OnClientUploadStarted="StartUpload"
    OnClientUploadComplete="UploadComplete"
    CompleteBackColor="Lime" 
    UploaderStyle="Modern" 
    ErrorBackColor="Red" 
    UploadingBackColor="#66CCFF"
    ClientIDMode="AutoID" 
    EnableViewState="true" />
<asp:UpdatePanel runat="server">
    <ContentTemplate>
        <asp:Label ID="lblError" runat="server" ForeColor="Red" Visible="false" />
        <asp:Repeater ID="rptAttachments" 
              runat="server" 
              OnItemCommand="Uploader_ItemCommand">
            <ItemTemplate>
             <a href='#'><%#Eval("Filename") %></a>  
             <asp:LinkButton ID="lnkDelete" 
                 runat="server" Text="Удалить"
                 CommandName="DeleteAttachment"
                 CommandArgument='<%#Eval("FileName") %>' 
              /> 
            </ItemTemplate>
        </asp:Repeater>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="uploader1" EventName="UploadedComplete" />
    </Triggers>
</asp:UpdatePanel>







    void AsyncFileUpload1_UploadComplete(object sender, AsyncFileUploadEventArgs e)
        {
           if (e.state == AsyncFileUploadState.Success)
              {

                if (!Facade.Attachment.UploadAttachment(attachment))
                   ShowErrorMessage("File already exists");   
                else
                   BindAttachments();
               }
         }

   void BindAttachments()
        {
            rptAttachments.DataSource = Facade.Attachment.GetAttachments(AttachmentId2);
            rptAttachments.DataBind();
        }

The event AsyncFileUpload1_UploadComplete causes, but nothing happens.

回答1:

The matter is OnUploadedComplete event is asynchronous and you can't make any changes in page. I had such problem later and as you can see, no answers...
I've found a workaround for me. I set in the server handler cookies with information, that I need and then read it in the client function and do appropriate action.
You can try, for example, after getting cookies on client side, send request to refresh page.