<pre id=\"best-answer-content\" class=\"reply-text mb10\">protected void Button3_Click(object sender, EventArgs e)
{
string strfilepath = Path.GetFileName(this.FileUpload1.PostedFile.FileName);
if (strfilepath.Length0)
{
upfile();
Response.Redirect(Request.Url.ToString());
}
else
{
Label3.Text = \"上传文件不能为空\";
}
}
private void upfile()
{
string filepath = Server.MapPath(\"./\") \"file\";
HttpFileCollection hfc = Request.Files;
for (int i = 0; ihfc.Count; i)
{
HttpPostedFile userhpf = hfc;
try
{
if (userhpf.ContentLength0)
{
userhpf.SaveAs(filepath \"\\\\\" Path.GetFileName(userhpf.FileName));
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings[\"ConnectionStr\"]);
con.Open();
SqlCommand cmd = new SqlCommand(\"insert into files values (@filename,@filedec,@filepath,@filetime)\", con);
cmd.Parameters.Add(\"@filename\", SqlDbType.VarChar, 50);
cmd.Parameters.Add(\"@filedec\", SqlDbType.VarChar, 200);
cmd.Parameters.Add(\"@filepath\", SqlDbType.VarChar, 100);
cmd.Parameters.Add(\"@filetime\", SqlDbType.DateTime);
cmd.Parameters[\"@filename\"].Value = TextBox1.Text;
cmd.Parameters[\"@filedec\"].Value = TextBox2.Text;
cmd.Parameters[\"@filepath\"].Value = filepath \"\\\\\" Path.GetFileName(userhpf.FileName);
cmd.Parameters[\"@filetime\"].Value = System.DateTime.Now;
cmd.ExecuteNonQuery();
con.Close();
}
}
catch
{
Label3.Text = \"上传失败\";
}
if (Session[\"FilesControls\"] != null)
{
Session.Remove(\"FilesControls\");
}
Label3.Text = \"上传成功\";
}
} |