【问题标题】:Setting file to FileLoad in is not working (C# .net)将文件设置为 FileLoad in 不起作用(C# .net)
【发布时间】:2014-06-23 23:47:55
【问题描述】:

我在“手动”将文件设置为 FileLoad 时遇到问题。

这是我的情况:

我正在使用这本手册:http://www.codeproject.com/Tips/101834/How-to-Maintain-FileUpload-Control-s-State-after-P?msg=4176652#xx4176652xx

我需要记住页面回发后加载的文件。所以我有 FileLoad 对象和 Button 来做 PostBack。回发后,我设置了 Session["MenuFile"] = FileLoad;,创建了会话记录,但是,当我尝试将此文件设置回 FileLoad 对象时,它真的到了那里(我可以在 LoadFile 对象上看到文件)。但在页面上它是空的。我什至尝试在 FileLoad 对象上的 Load、Init 事件上设置它,但没有任何效果。

这就像 FileLoad 从会话中成功加载文件,然后“重置”为默认设置(空白)。

这是我的代码:

     <tr>
        <td>
            <asp:Label runat="server" Text="Menu:"></asp:Label></td>
        <td>
            <asp:FileUpload runat="server" ID="fuMenu"/>
            <asp:RequiredFieldValidator runat="server" ID="rfvMenu" ControlToValidate="fuMenu" ErrorMessage="Menu file is required" ForeColor="Red">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Button runat="server" ID="neco" />
            <asp:ValidationSummary runat="server"/>
        </td>
    </tr>

以及后面的代码:

protected void Page_Load(object sender, EventArgs e)
    {

        // If first time page is submitted and we have file in FileUpload control but not in session 
        // Store the values to Session Object 
        if (Session["MenuFile"] == null && fuMenu.HasFile)
        {
            Session["MenuFile"] = fuMenu;

        }
        // Next time submit and Session has values but FileUpload is Blank 
        // Return the values from session to FileUpload 
        else if (Session["MenuFile"] != null && (!fuMenu.HasFile))
        {
            fuMenu = (FileUpload)Session["MenuFile"];

        }
        // Now there could be another sictution when Session has File but user want to change the file 
        // In this case we have to change the file in session object 
        else if (fuMenu.HasFile)
        {
            Session["MenuFile"] = fuMenu;
        }
    }

我需要使用会话,所以我不能使用这样的东西: How to Maintain FileUpload Control’s State after PostBack Information disappears on button click

我真的很绝望,我会很乐意提供任何帮助。 谢谢!

【问题讨论】:

  • 您使用的是UpDatePanel 吗?
  • 是的,我忘了提。我试过了,不管有没有,结果都是一样的。 :(

标签: c# file-upload postback


【解决方案1】:

出于安全原因,ASP:FileUpload 在设计上是只读的。回发后您不能在其中设置任何内容。

我在回发后使用标签而不是文件上传来向用户反馈已上传文件。

我的回发检查文件上传的内容,上传文件,隐藏文件上传,显示包含文件名的标签(存储在会话对象中),还显示一个隐藏标签的删除按钮,删除会话对象,删除文件并在另一次回发后再次显示文件上传。

【讨论】:

    猜你喜欢
    • 2020-12-26
    • 2017-12-10
    • 1970-01-01
    • 2011-06-09
    • 2016-03-08
    • 2015-09-23
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    相关资源
    最近更新 更多