文件上传不读取正在上传的文件

File Upload doesn't read the file being uploaded

我正在尝试使用文件上传来上传 excel 文件,但每当我这样做时 "If (FileUpload.HasFile)" 它总是 return false 即使我已经选择了一个文件。下面是我点击上传按钮时的代码。

(已编辑:当我上传文件时 if 语句最终变为真另一个问题是此错误消息显示 "The SaveAs method is configured to require a rooted path, and the path '~/Datas/Book1.xlsx' is not rooted." )

Blockquote

  protected void btnUpload_Click(object sender, EventArgs e)
{
        if (FileUpload1.HasFile)
        {
            try
            {
            FileUpload1.SaveAs("~/Datas/" + FileUpload1.FileName);
            Label1.Text = "File name: " +
                FileUpload1.PostedFile.FileName + "<br>" +
                FileUpload1.PostedFile.ContentLength + " kb<br>" +
                "Content type: " +
                FileUpload1.PostedFile.ContentType;
            string FileName = FileUpload1.FileName;
            string path = string.Concat(Server.MapPath("~/Datas/" + FileUpload1.FileName));

            FileUpload1.PostedFile.SaveAs(path);

            OleDbConnection OleDbcon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;");

            OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", OleDbcon);
            OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(cmd);
            DataSet ds = new DataSet();
            //Dt.Clear(); ds.Clear();
            objAdapter1.Fill(ds);
            DataTable Dt = new DataTable();
            Dt = ds.Tables[0];
            DataColumn dc = Dt.Columns.Add("Ser", typeof(Int32));
            int count = 0;
            foreach (DataRow item in Dt.Rows)
            {
                count++;
                item["Ser"] = count;
            }

            TraineeGrid.DataSource = Dt;
            TraineeGrid.DataBind();
        }
             catch (Exception ex)
           {
                 Label1.Text = "ERROR: " + ex.Message.ToString();
           }
       }

        else
        {
            Label1.Text = "You have not specified a file.";
        }
    }

I have this table that consists of buttons and this is where the file upload and upload button located

 <table>
            <tr>
                <td>
                    <asp:Button UseSubmitBehavior="false" CssClass="btnStyle" ID="enableFilter" runat="server" Text="Search" Width="100px" OnClick="filterMode_Click" />
                </td>
                <td>
                    <asp:Button UseSubmitBehavior="false" CssClass="btnStyle" ID="newTraineeBtn" runat="server" Text="Add" width="100px" OnClick="newTraineeBtn_Click" />
                </td>
                <td>
                    <asp:Button UseSubmitBehavior="false" CssClass="btnStyle" ID="enableDelete" runat="server" Text="Delete" width="100px" OnClientClick ="if (!confirm ('Are you sure you want to delete this row?')) return false;" OnClick="enableDelete_Click" />
                </td>
                <td>
                    <asp:Button UseSubmitBehavior="false" CssClass="btnStyle" ID="enableEdit" runat="server" Text="Edit" Width="100px" OnClick="enableEdit_Click" />     
                </td>
                <td>
                    <asp:Button UseSubmitBehavior="false" CssClass="btnStyle" ID="showColList"  runat="server" Text="Columns" OnClientClick="javascript:openPopup('ColumnList.aspx', 'xtf', '500', '405')" />
                </td>
                <td>
                    //file upload happens here 
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                    <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" />

             // I tried to add this code because they said it might work but it actually doesn't
                    <Triggers>
                        <asp:PostBackTrigger ControlID="btnUpload" />
                    </Triggers>
                </td>
            </tr>
        </table>

由于某些安全原因,FileUpload 控件将不会在回发期间保留该值。

一种方法是为上传按钮设置触发器

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

 <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
      <ContentTemplate>
      <asp:FileUpload ID="fileUpload" runat="server"></asp:FileUpload>
      <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" />
      </ContentTemplate>
      <Triggers>
          <asp:PostBackTrigger ControlID="btnUpload"  />
      </Triggers>
 </asp:UpdatePanel>

 protected void btnUpload_Click(object sender, EventArgs e)
     {
        if (fileUpload1.HasFile)
        {                
            fileUpload1.SaveAs("C:\" + fileupload1.FileName);
        }
     }

我已经尝试过,我能够获取文件。

<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnok" runat="server" Text="Ok"  OnClick="btnok_Click"/>

下面的事件是针对按钮的 click.May 我知道你在哪个地方遇到问题。

 protected void btnok_Click(object sender, EventArgs e)
   {
     if(FileUpload1.HasFile)
      {
        var val=FileUpload1.PostedFile;
      }
   }