How to upload all files in FileUpload controls in ASP.NET Repeater items

asp.netfile uploadpostbackrepeater

I have an ASP.NET Repeater control with a FileUpload Control in its item template. The ASP.NET Repeater is inside an UpdatePanel as part of a wizard made from an ASP.NET Multiview. I am trying to upload every file in each FileUpload control when I click submit, but the FileUpload controls do not retain a file when I submit the form. Here is the relevant code:

<asp:Repeater ID="RepeaterImages" runat="server" OnItemCommand="RepeaterBoxArts_ItemCommand">
  <ItemTemplate>
    <tr>
       <td class="right">
          Choose File:
       </td>
       <td>
           <asp:FileUpload ID="FileUpload" runat="server" />
       </td>
    </tr>
  </ItemTemplate>

foreach (RepeaterItem item in RepeaterImages.Items)
    {
        if (item.ItemType == ListItemType.Item
            || item.ItemType == ListItemType.AlternatingItem)
        {
            FileUpload fupload = (FileUpload)item.FindControl("FileUpload");
             if (fupload.HasFile)
            {
                string path = Server.MapPath("~/images/");
                fupload.SaveAs(path);

            }
        }
    }

When I click Submit, I want all FileUpload controls with a file to upload their files. However, if I run the step debugger, it shows each FileUpload control as not having a file. It seems to be an issue with postbacks, but I'm not 100% sure.

Best Answer

I had the same problem and solved adding this code

OnClientClick="javascript:document.forms[0].encoding = 'multipart/form-data';"

at the button that will upload all images.

The answer of this problem, I found at this link https://stackoverflow.com/a/217722

See my code below:

In the page ASPX

<asp:Repeater ID="rptUpload" runat="server" OnItemDataBound="rptUpload_ItemDataBound">
                            <HeaderTemplate>
                                <table width="100%">
                            </HeaderTemplate>
                            <ItemTemplate>
                                <tr>
                                    <td>
                                        <asp:FileUpload ID="fu" runat="server" />
                                    </td>
                                </tr>
                            </ItemTemplate>
                            <FooterTemplate>
                                </table>
                            </FooterTemplate>
                        </asp:Repeater>

                        <p>
                            <asp:ImageButton ID="btnUpload" runat="server" OnClick="btnUpload" ImageUrl="img/icon_upload.png" OnClientClick="javascript:document.forms[0].encoding = 'multipart/form-data';" />
                        </p>

In the CodeBehind

protected void btnUpload_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            foreach (RepeaterItem item in rptUpload.Items)
            {
                if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
                {
                    FileUpload fu = (FileUpload)item.FindControl("fu");
                    if (fu.HasFile)
                    {
                        string path = Server.MapPath("~/images/");
                        string fileName = Path.GetFileName(fu.FileName);
                        string fileExt = Path.GetExtension(fu.FileName).ToLower();
                        fu.SaveAs(path + fileName + fileExt);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
Related Topic