简体   繁体   English

ASP.NET多个文件上传

[英]ASP.NET Multiple Fileupload

I'm building a website (umbraco based) where the users are able to upload multiple images to their posts. 我正在建立一个网站(基于umbraco),用户可以在其中将多个图像上传到他们的帖子中。 What I have so far is: 到目前为止,我有:

<asp:TextBox MaxLength="1" Width="29px" runat="server" ID="txtImageAmount" />&nbsp;
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
       <asp:Button ID="btnSubmitImageAmount" runat="server" Text="Vis upload felter" 
            onclick="btnSubmitImageAmount_Click" />

       <asp:Label Visible="false" ID="lblImageAmountError" ForeColor="Red" runat="server" Text="Maks 3 billeder"></asp:Label>  
       <asp:Panel ID="pnlUploadControls" Visible="false" runat="server"></asp:Panel>
    </ContentTemplate>
</asp:UpdatePanel>  

So the user is able to choose the amount of fileupload boxes (max 3 though) on the site. 因此,用户可以在站点上选择文件上传框的数量(尽管最多3个)。

My C# looks like this: 我的C#看起来像这样:

protected void btnSubmitImageAmount_Click(object sender, EventArgs e)
{
    int amountOfControls = Convert.ToInt32(txtImageAmount.Text);
    if (amountOfControls <= 3)
    {
        for(int i = 0; i < amountOfControls; i++)
        {
            FileUpload fUl = new FileUpload();
            fUl.ID = i.ToString();
            fUl.Width = 300;
            Label lblLinebreak = new Label();
            lblLinebreak.Text = "<br />";
            pnlUploadControls.Controls.Add(fUl);
            pnlUploadControls.Controls.Add(lblLinebreak);
            pnlUploadControls.Visible = true;
        }
    }
    else
    {
        lblImageAmountError.Visible = true;
    }
}

So basically I'm adding a new FileUpload control to the Panel depending on how many the user wants. 因此,基本上我要根据用户需要的数量向面板添加一个新的FileUpload控件。

Now, in my Save button I have the following code: 现在,在“保存”按钮中,我有以下代码:

List<Media> images = new List<Media>();

    foreach (FileUpload fUl in pnlUploadControls.Controls)
    {
        Media m = UmbracoSave(fUl);
        if (m != null)
        {
            images.Add(m);
        }       
    }

    if (images.Count > 0)
    {
        RelationType ad2media = RelationType.GetByAlias("ad2media");
        foreach (Media img in images)
        {
            Relation.MakeNew(adDoc.Id, img.Id, ad2media, adDoc.Text + " is related to " + img.Text);
        }
    }

I have tried to check if the amount of controls in the panel is equal to 0 and it seems it is.. The weird thing is, if I check if the Media item returned from the UmbracoSave method is null, it isn't. 我试图检查面板中的控件数量是否等于0,并且看起来是..奇怪的是,如果我检查从UmbracoSave方法返回的Media项是否为null,则不是。

Also, it says that the List (images) count is 0.. 此外,它说列表(图像)计数为0。

Can anyone shed some light on this? 谁能对此有所启发? :-) :-)

Any help is greatly appreciated! 任何帮助是极大的赞赏!

All the best, 祝一切顺利,

Bo

You can't put a standard FileUpload control in an UpdatePanel. 您不能在UpdatePanel中放置标准的FileUpload控件。 It just doesn't work. 就是行不通。 Look at an AJAX compatible file upload component. 查看兼容AJAX的文件上传组件。

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

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