【问题标题】:Upload file control not showing any file in the code behind上传文件控件在后面的代码中不显示任何文件
【发布时间】:2020-07-23 00:04:43
【问题描述】:

我有一个非常简单的应用程序。我正在尝试使用 ASP.net 的文件上传控件上传文件。下面是我的整个 .cs 代码和 .aspx 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestFileUpload1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void uploadFile_Click(object sender, EventArgs e)
        {

            if (UploadImages.HasFiles)
            {
                foreach (HttpPostedFile uploadedFile in UploadImages.PostedFiles)
                {
                    uploadedFile.SaveAs(System.IO.Path.Combine(Server.MapPath("~/Images/"), uploadedFile.FileName));
                    //listofuploadedfiles.Text += String.Format("{0}<br />", uploadedFile.FileName);
                }
            }
        }
    }
}

我的 .aspx 代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestFileUpload1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Test web site</title>
       <script src="Scripts/jquery-1.11.0.js"></script>

  <script src="Scripts/jquery.mobile-1.4.5.js"></script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
              <div>
             <asp:FileUpload runat="server" ID="UploadImages" AllowMultiple="true" />  
            <asp:Button runat="server" ID="uploadedFile" Text="Upload" OnClick="uploadFile_Click" />  
             <asp:Label ID="listofuploadedfiles" runat="server" />  
        </div>
        </div>
    </form>
</body>
</html>

每当我尝试上传文件时,UploadImages.HasFiles 都会收到“False”。 以上是完整的工作示例。 一旦我删除这些脚本标签之一:

 <script src="Scripts/jquery-1.11.0.js"></script>

  <script src="Scripts/jquery.mobile-1.4.5.js"></script>

我的代码开始工作,当我尝试上传文件时,UploadImages.HasFiles 为“true”。

我正在使用 .net 框架 4.7.2

由于 GUI 的原因,我需要将这两个脚本标签保留在我的代码中,这是一个旧应用程序,所有页面都使用了这些标签。

我还尝试将控件包装在更新面板中,但这也不起作用。下面是更改后的 .aspx 页面。虽然,我希望我的原始代码能够工作。我不想使用ajax,但我只是尝试使用它,因为它被建议作为解决方案之一

  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestFileUpload1.WebForm1" %>
<%--<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit"  TagPrefix="asp"%>--%>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Test web site</title>
       <script src="Scripts/jquery-1.11.0.js"></script>

  <script src="Scripts/jquery.mobile-1.4.5.js"></script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <div>
              <div>
                  <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                        <ContentTemplate>
                            <asp:FileUpload runat="server" ID="UploadImages" AllowMultiple="true" />
                        </ContentTemplate>
                       <Triggers>
                           <asp:PostBackTrigger ControlID="uploadedFile" />
                        </Triggers>
                      
                      </asp:UpdatePanel>
              <asp:Button runat="server" ID="uploadedFile" Text="Upload" OnClick="uploadFile_Click" />
             <asp:Label ID="listofuploadedfiles" runat="server" />  
                      
        </div>
        </div>
    </form>
</body>
</html>

下面是我在后面的代码中得到的错误值的图像:

我们将不胜感激。

【问题讨论】:

    标签: c# jquery asp.net


    【解决方案1】:

    我所要做的就是将 data-ajax ="false" 放在表单标签中,这样就解决了问题。

      <form id="form1" runat="server" data-ajax ="false">
    

    【讨论】:

      【解决方案2】:

      使用更新面板并将您的控件包装在其中。然后将按钮 controlID 作为触发器(确保它是 PostBackTrigger)添加到更新面板。要进行测试,请务必在 uploadFile_Click 事件上设置一个断点,以便您可以单步执行并查看值..

                       <asp:UpdatePanel ID="UpdatePanel1" runat="server"     UpdateMode="Conditional">
        <Triggers>
      <asp:PostBackTrigger ControlID="btnSignaureOfRequestor" />
      </Triggers>
         <ContentTemplate>
             <table>
                  <tr>  <td class="tdText" colspan="4">
                 <asp:FileUpload ID="fileUpload" runat="server" Width="50%" />
                          </td>
                                  </tr>
      <tr>
          <td> <asp:Button ID="btnSignaureOfRequestor" runat="server"  Text="Submit Request" Visible="true" OnClientClick="return confirm('Are  you sure you want to continue?');" OnClick="btnSignaureOfRequestor_Click" />
                                     </td>
         </tr>                                  
                           </table>
                  </ContentTemplate>
                  </asp:UpdatePanel>
      

      【讨论】:

      • 不,这不起作用。我在上面粘贴了更新后的 .aspx 文件,这对于 hasfiles 也返回 false。
      • 不要使用 AsyncPostBackTrigger 使用 PostBackTrigger,只有 PostBackTrigger 可以工作
      • 我尝试使用 postBackTriggers,但在后面的代码中仍然出现错误。我也粘贴了代码的图片。以上是一个完整的工作示例。您可以将上述代码复制并粘贴到任何 asp.net 网络表单中,并且可以复制问题。
      • 尝试将按钮放在更新面板的内容模板中
      • 执行此操作时,我开始收到错误消息:在 UpdatePanel 'UpdatePanel1' 中找不到触发器的 ID 为 'uploadedFile' 的控件。
      【解决方案3】:

      尝试以下,删除允许多个并添加更新模式条件(请参阅确认这些更改的大小写/语法,因为我只是在记事本中编写它)

       <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
          <div>
                <div>
                    <asp:UpdatePanel ID="UpdatePanel1" runat="server" updateMode="Conditional">
                          <ContentTemplate>
                              <asp:FileUpload runat="server" ID="UploadImages" />&nbsp;
           <asp:Button runat="server" ID="uploadedFile" Text="Upload" OnClick="uploadFile_Click" />
                          </ContentTemplate>
                         <Triggers>
                             <asp:PostBackTrigger ControlID="uploadedFile" />
                          </Triggers>                   
                        </asp:UpdatePanel>
       
               <asp:Label ID="listofuploadedfiles" runat="server" />  
                        
          </div>
          </div>
      

      【讨论】:

      • 这段代码对我没有帮助,因为我的要求是客户可以选择多个文件。我还尝试了您在上面给我的代码,但没有用。我仍然对 hasFiles 感到错误
      • 代码在我这边工作,不知道为什么不适合你......有很多方法可以满足要求。注明是否有多个文件让用户压缩它们。或者您可以向页面添加多个文件上传。您还可以使用文件上传构建一个单独的页面并在模式对话框中打开它。
      猜你喜欢
      • 1970-01-01
      • 2012-04-27
      • 2012-09-06
      • 1970-01-01
      • 2014-05-04
      • 1970-01-01
      • 2019-11-09
      • 2013-08-13
      • 1970-01-01
      相关资源
      最近更新 更多