【问题标题】:Why does my File.HasFile always return false为什么我的 File.HasFile 总是返回 false
【发布时间】:2009-12-22 01:12:35
【问题描述】:

用户将输入客户编号,然后选择要上传的文件,然后输入描述并按保存按钮。但是,当我检查他们是否选择了一个文件时,它总是返回 false!我想知道为什么。

编辑:我现在使用带有参数 enctype="multipart/form-data" 的表单,它包含在更新面板中。表单和更新面板都在母版页中

这是错误处理并获取 HasFile() 值的代码的 sn-p:

else if ( ( images.Selected == true || docs.Selected == true ) && 
                  ( Upload.HasFile == false || txtInfo.Text == string.Empty ) )
        {
            if ( Upload.HasFile == false )
                lblErrorMessage.Text = "You haven't selected a file";
            else lblErrorMessage.Text = "You must enter a description";

            validated = false;
        }

这是来自的方法,保存按钮

protected void btnSaveNew_Click( object sender, EventArgs e )
    {
        bool validated = true;

        lblErrorMessage.ForeColor = System.Drawing.Color.Red;

        if ( txtCustomerNumber.Text.Length != 8 )
        {
            if ( txtCustomerNumber.Text.Length == 0 )
                lblErrorMessage.Text = "Hey! What about the customer number?";
            else if ( txtCustomerNumber.Text.Length > 8 )
                lblErrorMessage.Text = "Invalid Customer Number length (" + ( txtCustomerNumber.Text.Length - 8 ) + " char(s) too long)";
            else lblErrorMessage.Text = "Invalid Customer Number length (" + ( 8 - txtCustomerNumber.Text.Length ) + " char(s) short)";

            validated = false;
        }

        else if ( links.Selected == true && ( txtLink.Text == string.Empty || txtInfo.Text == string.Empty ) )
        {
            if ( txtLink.Text == string.Empty )
                lblErrorMessage.Text = "Hey! You forgot to enter a link!";
            else lblErrorMessage.Text = "You must enter a description";

            validated = false;
        }

        else if ( ( images.Selected == true || docs.Selected == true ) && 
                  ( Upload.HasFile == false || txtInfo.Text == string.Empty ) )
        {
            if ( Upload.HasFile == false )
                lblErrorMessage.Text = "You haven't selected a file";
            else lblErrorMessage.Text = "You must enter a description";

            validated = false;
        }

        else if ( txtInfoDesc.Text == string.Empty || txtInfo.Text == string.Empty )
        {
            if ( txtInfoDesc.Text == string.Empty )
                lblErrorMessage.Text = "You must enter an info description";
            else lblErrorMessage.Text = "You must enter info Data";

            validated = false;
        }

        if ( validated == false )
        {
            btnModify.Visible           = false;
            btnCreateNew.Visible        = false;
            ddlCustomerNumber.Visible   = false;
            btnSaveNew.Visible          = true;
            btnCancel.Visible           = true;
            txtCustomerNumber.Visible   = true;

            switch ( rblSection.SelectedIndex )
            {
                case 0: txtInfo.Visible = true;
                        txtLink.Visible = true;
                        break;
                case 1: 
                case 2: txtInfo.Visible = true;
                        Upload.Visible  = true;
                        break;
                case 3: txtInfo.Visible     = true;
                        txtInfoDesc.Visible = true;
                        break;
            }

            if ( GetCategoryIDCookie() != 0 )
                divData.Attributes.Add( "Style", "overflow:auto" );
        }
        else
        {
            addNewCustomerNumber( txtCustomerNumber.Text, txtInfoDesc.Text, txtInfo.Text );
            ddlCustomerNumber.Visible = true;
            divData.Attributes.Remove( "Style" );
            Response.Redirect( Request.RawUrl );
        }
    }

【问题讨论】:

  • 你是不是碰巧在更新面板里
  • 你的表单标签上有 enctype="multipart/form-data" 吗?
  • @Jeffrey - 我没有,但将它添加到我的表单参数并没有改变我的返回值

标签: c# asp.net file-upload


【解决方案1】:

文件上传是否有更新面板?

【讨论】:

  • 是的。此外,此 updatePanel 位于主文件中,此后我了解到我需要执行 PostBackTriggers 但我找不到一篇文章来解释如何在主文件中执行此操作,因为我不断收到无法找到的错误触发。
  • 我最终只是删除了更新面板,这解决了我的问题。这段代码是前人做过的,功能上好像是一模一样的。
【解决方案2】:

你的表单标签上有 enctype="multipart/form-data" 吗?

【讨论】:

  • 我没有,但添加这个没有帮助。
【解决方案3】:

您可以发布 Upload 类的代码吗?也许您错误地将该变量初始化为 false,并且在某些事件中忘记将其设置为 true。

【讨论】:

  • 我不确定我是否理解。我正在尝试在上传到数据库之前检查是否有文件。我的代码中的上传名称(例如 Upload.HasFile)是指我的 asp:FileUpload 对象的 ID
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多