C# 为什么my File.HasFile总是返回false
用户将输入客户编号,然后选择要上载的文件,然后输入说明并按下保存按钮。然而,当我检查他们是否选择了一个文件时,它总是返回false!我想知道为什么 编辑:我现在使用带有参数enctype=“multipart/form data”的表单,它包装在更新面板中。表单和更新面板都位于母版页中 以下是错误处理并获取HasFile()值的代码片段: 这里有一个方法,保存按钮C# 为什么my File.HasFile总是返回false,c#,asp.net,file-upload,C#,Asp.net,File Upload,用户将输入客户编号,然后选择要上载的文件,然后输入说明并按下保存按钮。然而,当我检查他们是否选择了一个文件时,它总是返回false!我想知道为什么 编辑:我现在使用带有参数enctype=“multipart/form data”的表单,它包装在更新面板中。表单和更新面板都位于母版页中 以下是错误处理并获取HasFile()值的代码片段: 这里有一个方法,保存按钮 protected void btnSaveNew_Click( object sender, EventArgs e )
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”标签?您的表单标签上是否有enctype=“multipart/form data”标签?您可以发布上传类的代码吗?可能您错误地将该变量初始化为false,并且在某些事件中忘记将其设置为true。您可以发布Upload类的代码吗?可能是您错误地将该变量初始化为false,并在某些事件中忘记将其设置为true。您是否碰巧在更新面板中您是否有enctype=“multipart/form data”在您的表单标签上?@Jeffrey-我没有,但将其添加到我的表单参数并没有改变我的返回值您是否碰巧在更新面板中您的表单标签上是否有enctype=“multipart/form data”?@Jeffrey-我没有,但将其添加到我的表单参数并没有改变我的返回值我不确定我是否理解。在上传到数据库之前,我正在检查是否有文件。我代码中的上载名称(例如Upload.HasFile)指的是我的asp:FileUpload对象的ID,我不确定我是否理解。在上传到数据库之前,我正在检查是否有文件。我的代码中的上载名称(例如Upload.HasFile)指的是我的asp:FileUpload对象的ID。我没有,但是添加这个没有帮助。我没有,但是添加这个没有帮助。是的。另外,这个updatePanel在主文件中,我已经知道我需要执行PostBackTriggers,但是我找不到一篇文章来解释如何在主文件中执行它,因为我一直收到一个错误,它找不到触发器。我只是删除了更新面板,这就解决了我的问题。这段代码是由以前的人编写的,功能似乎完全相同。是的。另外,这个updatePanel在主文件中,我已经知道我需要执行PostBackTriggers,但是我找不到一篇文章来解释如何在主文件中执行它,因为我一直收到一个错误,它找不到触发器。我只是删除了更新面板,这就解决了我的问题。这段代码是由以前的人编写的,功能似乎完全相同。
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 );
}
}