0

我想使用 FileUpload Control 将图像保存到数据库。单击 BtnSave 后,HasFiles 总是返回 false。我尝试使用和不使用 UpdatePanel,但找不到解决方案。

我的代码是:

  protected void BtnSave_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile == true)
        {
            var ChNumber = txtNumber.Text;

            string img = this.FileUpload1.PostedFile.FileName;
  //Save image to database function
        }
 else
 {
  lblStatus.Text="Image not Uploaded";
 }
 }
4

1 回答 1

0

尝试使用没有 UpdatePanel 的简单表单。这是标记:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="BtnSave" runat="server" Text="Button" OnClick="BtnSave_Click" />
        <asp:TextBox ID="txtNumber" runat="server"></asp:TextBox>
        <asp:Label ID="lblStatus" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

这是代码:

using System;

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

    protected void BtnSave_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile == true)
        {
            var ChNumber = txtNumber.Text;

            string img = this.FileUpload1.PostedFile.FileName;
            //Save image to database function
        }
        else
        {
            lblStatus.Text = "Image not Uploaded";
        }
    }
}

我设置了一个断点,if (FileUpload1.HasFile == true)并看到上传的文件在那里。

于 2013-10-12T15:32:09.813 回答