Question

I'm using an asyncFileUpload control from the AJAX control toolkit. I can typecast it in code behind and access it, but it doens't seem to grab a file. this is how it's nested in the page:

<asp:LoginView ID="LoginView1" runat="server">
    <AnonymousTemplate>
    </AnonymousTemplate>
    <LoggedInTemplate>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Repeater ID="rpt_items_loggedin" runat="server">
                   <ItemTemplate>
                   </ItemTemplate>
                </asp:Repeater>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="btn_addwish" EventName="click" />
            </Triggers>
        </asp:UpdatePanel>
        <div class="wenslijst_preview">        
            <div class="fourfifth">
                <table>
                    <tr>
                       <td>
                          <asp:Label ID="lbl_img" runat="server" Text="Upload een afbeelding:" AssociatedControlID="fu_img_upload"></asp:Label>
                       </td>
                       <td>
                          <asp:AsyncFileUpload ID="asfu_img" runat="server" UploaderStyle="Traditional" />
                       </td>
                    </tr>
                </table>
            </div>
            <div class="fifth">
                <asp:ImageButton ID="btn_addwish" runat="server" CssClass="icon_img" 
                                        ImageUrl="images/add.png" onclick="btn_addwish_Click"/>
            </div>
        </div>  
     </LoggedInTemplate>
</asp:LoginView> 

And this is how I handle it in code behind. When I debug I see that the AsyncFileUpload control doens't get any data:

protected void btn_addwish_Click(object sender, ImageClickEventArgs e)
{
        AjaxControlToolkit.AsyncFileUpload f = new      AjaxControlToolkit.AsyncFileUpload();
        f = (AjaxControlToolkit.AsyncFileUpload)(this.LoginView1.FindControl("asfu_img"));
        Label l = new Label();
        l = (Label)(this.LoginView1.FindControl("lbl_img_feedback"));
        string filePath = "";
        if (f.HasFile)
        {
            try
            {
                string fileName = DateTime.UtcNow.Ticks + "_" + cId;
                filePath = "images/userimg/" + fileName;
                f.SaveAs(Server.MapPath(filePath));
                l.Text = filePath;
            }
            catch (Exception ex)
            {
                l.Text = "Deze foto kon niet worden geuploaded. " + ex.Message;
            }
        }
    }

Tell me if you need to see more code or info!

Was it helpful?

Solution

Make sure that the usercontrol with asyncfileupload control is not loaded asynchronously, for example via Response.Redirect("pageWithUploadControl").

Have you handled the FileUploadComplete Event and checked if AsyncFileUploadState is Success?

also try to change change the enctype of your form:

<form id="form1" enctype="multipart/form-data" runat="server">

OTHER TIPS

It seems to me that btn_addwish is the button which you want to use in order to upload the file, if that's the case then your problem is that when the f.Hasfile is executed the upload hasn't finished, hence, it doesn't has a file. Try to use UploadedComplete event or somehow another control to check the hasfile.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top