利用HTml中的file控件后,如何将图片保存在指定文件夹下
展开全部
//上传文件
protected void bt_Post_Click(object sender, EventArgs e)
{
string strBaseLocation = Server.MapPath("../EmployeeFile/");
string TruePath = string.Empty;
string Temp1 = string.Empty;
string remarkid = string.Empty;
if (uploadFile.PostedFile.ContentLength != 0)
{
//获得文件全名
string fileName = System.IO.Path.GetFileName(uploadFile.PostedFile.FileName);
//获得扩展名
string rightName = System.IO.Path.GetExtension(fileName);
Random g = new Random();
string rad = g.Next(10000).ToString();
Temp1 = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + rad;
uploadFile.PostedFile.SaveAs(strBaseLocation + Temp1 + rightName);
TruePath = Temp1 + rightName; //获得随即文件名
if (Request.QueryString["id"] != "0")
{
string sql_sel = "select * from tsmannouncementmanage where id='" + Request.QueryString["id"] + "'";
OleDbDataReader dr = list.GetList(sql_sel);
if (dr.Read())
{
remarkid = dr["ContactFile"].ToString();
}
if (remarkid != "")
{
string sql_update_file = "update tsmmainannouncefile set name='" + fileName + "',newname='" + TruePath + "' where keyfile='" + remarkid + "'";
list.ExeSql(sql_update_file);
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功!');</script>");
}
else
{
string sql_updateorder = "update tsmannouncementmanage set fileid='" + list.GetFormatStr(Number.Text) + "' where id='" + Request.QueryString["id"] + "'";
list.ExeSql(sql_updateorder);
string sql_insert_file = "insert into tsmmainannouncefile (Name,NewName,KeyFile) values ('" + fileName + "','" + TruePath + "','" + list.GetFormatStr(Number.Text) + "')";
list.ExeSql(sql_insert_file);
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('上传成功!');</script>");
}
}
else
{
string sql_insert_file = "insert into tsmmainannouncefile (Name,NewName,KeyFile) values ('" + fileName + "','" + TruePath + "','" + list.GetFormatStr(Number.Text) + "')";
list.ExeSql(sql_insert_file);
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('上传成功!');</script>");
}
}
else
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('你还没有选择要上传的附件!');</script>");
}
}
protected void bt_Post_Click(object sender, EventArgs e)
{
string strBaseLocation = Server.MapPath("../EmployeeFile/");
string TruePath = string.Empty;
string Temp1 = string.Empty;
string remarkid = string.Empty;
if (uploadFile.PostedFile.ContentLength != 0)
{
//获得文件全名
string fileName = System.IO.Path.GetFileName(uploadFile.PostedFile.FileName);
//获得扩展名
string rightName = System.IO.Path.GetExtension(fileName);
Random g = new Random();
string rad = g.Next(10000).ToString();
Temp1 = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + rad;
uploadFile.PostedFile.SaveAs(strBaseLocation + Temp1 + rightName);
TruePath = Temp1 + rightName; //获得随即文件名
if (Request.QueryString["id"] != "0")
{
string sql_sel = "select * from tsmannouncementmanage where id='" + Request.QueryString["id"] + "'";
OleDbDataReader dr = list.GetList(sql_sel);
if (dr.Read())
{
remarkid = dr["ContactFile"].ToString();
}
if (remarkid != "")
{
string sql_update_file = "update tsmmainannouncefile set name='" + fileName + "',newname='" + TruePath + "' where keyfile='" + remarkid + "'";
list.ExeSql(sql_update_file);
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功!');</script>");
}
else
{
string sql_updateorder = "update tsmannouncementmanage set fileid='" + list.GetFormatStr(Number.Text) + "' where id='" + Request.QueryString["id"] + "'";
list.ExeSql(sql_updateorder);
string sql_insert_file = "insert into tsmmainannouncefile (Name,NewName,KeyFile) values ('" + fileName + "','" + TruePath + "','" + list.GetFormatStr(Number.Text) + "')";
list.ExeSql(sql_insert_file);
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('上传成功!');</script>");
}
}
else
{
string sql_insert_file = "insert into tsmmainannouncefile (Name,NewName,KeyFile) values ('" + fileName + "','" + TruePath + "','" + list.GetFormatStr(Number.Text) + "')";
list.ExeSql(sql_insert_file);
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('上传成功!');</script>");
}
}
else
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('你还没有选择要上传的附件!');</script>");
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询