我正在尝试使用文件上传上传 excel 文件,但是每当我执行“If (FileUpload.HasFile)”时,它总是返回 false,即使我已经选择了一个文件。下面是我点击上传按钮时的代码。
(已编辑:当我上传文件时,if 语句最终变为真 另一个问题是此错误消息显示“SaveAs 方法配置为需要有根路径,而路径 '~/Datas/Book1.xlsx' 没有根。” )
块引用
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.";
}
}
我有一张由按钮组成的表格,这是文件上传和上传按钮所在的位置
交互式爱情
白衣非少年
相关分类