0

I'm adding at my ASPX page some controls dynamically. I need to add dynamically them, because the amount of controls depends on database record.

I'm using UpdatePanel and adding controls dynamically like that:

void AddFileUploadFields()
{
    for (int i = 0; i < Helper.uploadFieldsCount; i++)
    {
        FileUpload fileUpload = new FileUpload();
        string controlId = DateTime.Now.Ticks.ToString();
        Thread.Sleep(10); // for non-equal ID for each control
        fileUpload.ID = controlId;
        uploadFormsId.Add(controlId);

        UpdatePanel1.ContentTemplateContainer.Controls.Add(fileUpload);
        PlaceHolder1.Controls.Add(fileUpload);
     }
}

Then from the button, which was defined statically in asp-tag <asp:Button ... /> I'm trying to handle the file info, which I want to upload:

void buttonUpload_Click(object sender, EventArgs e)
{
    var iEnum = uploadFormsId.GetEnumerator();
    List<UploadFormDetails> uploadsInfo = new List<UploadFormDetails>();
    List<string> generatedFileNames = new List<string>();
    bool wereErrors = false;

    while (iEnum.MoveNext())
    {
        FileUpload uploadForm = (FileUpload)FindControl(iEnum.Current.ToString());
        uploadsInfo.Add(new UploadFormDetails(uploadForm.ID, uploadForm.HasFile, uploadForm.FileName));
    }
...
}

The main problem does occur here:

FileUpload uploadForm = (FileUpload)FindControl(iEnum.Current.ToString());

When I'm getting control like that, all options of it, like .HasFile or .FileName are unavailable to see. It just looks like a new control with no saved options. Of course, I'm selecting some file for the test, please don't think that I'm trying to upload from empty form.

How could i fix it? I can't upload any file...

4

1 回答 1

0

我找到了一个解决方案,但我得到的结果是以其他方式设计的,它不使用Server controlsASP.NET,我处理了普通HTML元素。

在 ASPX 页面中,我定义了:

<%
    for (int i = 0; i < Helper.uploadFieldsCount; i++)
    {
        Response.Write("<input type='file' id='uploadField" + i.ToString() + "' name='uploadField" + i.ToString() + "' class='file_1' /><br />");
    }
%>

runat在页面的主窗体中定义,一开始:

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

CodeBehind我正在上传一个随机生成的名称的新文件,基于GUID

void buttonUpload_Click(object sender, EventArgs e)
{
    List<string> generatedFileNames = new List<string>();
    bool wereErrors = false;

    for (int i = 0; i < Helper.uploadFieldsCount; i++)
    {
        HttpPostedFile filePosted = Request.Files["uploadField" + i.ToString()];

        if (filePosted != null && filePosted.ContentLength > 0)
        {
            string fileNameApplication = System.IO.Path.GetFileName(filePosted.FileName);
            string fileExtensionApplication = System.IO.Path.GetExtension(fileNameApplication);
            string newFile = Guid.NewGuid().ToString() + fileExtensionApplication;
            string filePath = System.IO.Path.Combine(Server.MapPath("uploads"), newFile);

            if (fileNameApplication != String.Empty)
            {
                generatedFileNames.Add(newFile);
                filePosted.SaveAs(filePath);
            }
       }
...
于 2013-04-13T18:57:31.700 回答