动态生成的文件上传给不了选项 [英] Dynamically generated FileUploads can't give options

查看:307
本文介绍了动态生成的文件上传给不了选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 ASPX 页一些控件动态添加。我需要动态添加它们,因为控制量取决于数据库记录。

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.

我用的UpdatePanel 并动态添加控件这样的:

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);
     }
}

再从按钮,这是静态在ASP标签定义的&LT; ASP:按钮... /&GT; 我想处理文件的信息,我要上传

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 =(文件上传)的FindControl(iEnum.Current.ToString());

当我得到这样的控制,它的所有选项,如 .HasFile .FileName 是无法看到的。它只是看起来像没有保存的选项一个新的控制。当然,我选择的测试某些文件时,请不要认为我试图从空的形式上传。

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

推荐答案

我发现了一个解决方案,但结果我有被设计在其他的方式,不使用服务器控件 ASP.NET中,我处理的纯 HTML 元素。

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.

在ASPX页面,我定义的:

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 在页面的主要形式定义,在开始的时候:

runat was defined in the main form of the page, in the beginning:

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

codeBehind 我上传与基于 GUID 随机生成的文件名的文件:

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);
            }
       }
...

这篇关于动态生成的文件上传给不了选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆