Dynamically generated FileUploads can't give options

杀马特。学长 韩版系。学妹 提交于 2019-12-25 02:50:24

问题


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...


回答1:


I found a solution, but the result I've got is designed in other way, which doesn't use Server controls of ASP.NET, I handled the plain HTML elements.

In ASPX page, I defined:

<%
    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 was defined in the main form of the page, in the beginning:

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

In the CodeBehind I'm uploading a new file with randomly generated name based on 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);
            }
       }
...


来源:https://stackoverflow.com/questions/15950638/dynamically-generated-fileuploads-cant-give-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!