
关于FileUpload上传图片后,自动刷新,路径为空,无法获得其值 10
我想从FileUpload上传图片后,保存其图片路径,然后存入数据库。可是每次点击上传按钮后,网页自动刷新,无法获得FileUpload的值。代码如下:上传和显示代码:p...
我想从FileUpload上传图片后,保存其图片路径,然后存入数据库。可是每次点击上传按钮后,网页自动刷新,无法获得FileUpload的值。代码如下:
上传和显示代码:protected void Button3_Click(object sender, EventArgs e)
{
string fullfileName = this.FileUpload1.PostedFile.FileName;
string fileName = fullfileName.Substring(fullfileName.LastIndexOf("\\") + 1);
string type = fullfileName.Substring(fullfileName.LastIndexOf(".") + 1);
if (type == "jpg" || type == "bmp" || type == "gif")
{
this.FileUpload1.PostedFile.SaveAs(Server.MapPath("up") + "\\" + fileName);
this.ImageButton1.ImageUrl = "up/" + fileName;
}
else
{
Response.Write("<script>alert('格式错误');</script>");
}
}
添加到数据库的代码:
protected void ad3_Click(object sender, EventArgs e)
{
string aName = this.aName.Text;
string aSex = "";
string aBirth = this.aBirth.Text;
string aWorktime = this.aWorktime.Text;
string aWorkplace = this.aWorkplace.Text;
string aPosition = this.aPosition.Text;
string aEducation = this.aEducation.Text;
string aChinese = this.aChinese.Text;
string aEnglish = this.aEnglish.Text;
string aAdd = this.aAdd.Text;
string aPhone = this.aPhone.Text;
string aCellphone = this.aCellphone.Text;
string aE = this.aE.Text;
string path = this.FileUpload1.PostedFile.FileName;
string aImagepath = "up\\"+path;
if (this.aMan.Checked == true)
{
aSex = this.aMan.Text;
}
else
{
aSex = this.aWoman.Text;
}
SqlConnection con = DB.creatConnection();
con.Open();
//(pName,pSex,pWorkplace,pPhone,pCellphone,pAdd,pE,pBirth,pWorktime,pPosition,pImage,pEducation,pChinese,pEnglish)
string insert = string.Format("insert into personInfo(pName,pSex,pWorkplace,pPhone,pCellphone,pAdd,pE,pBirth,pWorktime,pPosition,pEducation,pChinese,pEnglish,pImage) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}')", aName, aSex, aWorkplace, aPhone, aCellphone, aAdd, aE, aBirth, aWorktime, aPosition, aEducation, aChinese, aEnglish,aImagepath);
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(insert, con);
try
{
cmd.ExecuteNonQuery();
}
catch
{
}
con.Close();
}
aImagepath始终取不到值。
自己解决了,用session会话解决 展开
上传和显示代码:protected void Button3_Click(object sender, EventArgs e)
{
string fullfileName = this.FileUpload1.PostedFile.FileName;
string fileName = fullfileName.Substring(fullfileName.LastIndexOf("\\") + 1);
string type = fullfileName.Substring(fullfileName.LastIndexOf(".") + 1);
if (type == "jpg" || type == "bmp" || type == "gif")
{
this.FileUpload1.PostedFile.SaveAs(Server.MapPath("up") + "\\" + fileName);
this.ImageButton1.ImageUrl = "up/" + fileName;
}
else
{
Response.Write("<script>alert('格式错误');</script>");
}
}
添加到数据库的代码:
protected void ad3_Click(object sender, EventArgs e)
{
string aName = this.aName.Text;
string aSex = "";
string aBirth = this.aBirth.Text;
string aWorktime = this.aWorktime.Text;
string aWorkplace = this.aWorkplace.Text;
string aPosition = this.aPosition.Text;
string aEducation = this.aEducation.Text;
string aChinese = this.aChinese.Text;
string aEnglish = this.aEnglish.Text;
string aAdd = this.aAdd.Text;
string aPhone = this.aPhone.Text;
string aCellphone = this.aCellphone.Text;
string aE = this.aE.Text;
string path = this.FileUpload1.PostedFile.FileName;
string aImagepath = "up\\"+path;
if (this.aMan.Checked == true)
{
aSex = this.aMan.Text;
}
else
{
aSex = this.aWoman.Text;
}
SqlConnection con = DB.creatConnection();
con.Open();
//(pName,pSex,pWorkplace,pPhone,pCellphone,pAdd,pE,pBirth,pWorktime,pPosition,pImage,pEducation,pChinese,pEnglish)
string insert = string.Format("insert into personInfo(pName,pSex,pWorkplace,pPhone,pCellphone,pAdd,pE,pBirth,pWorktime,pPosition,pEducation,pChinese,pEnglish,pImage) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}')", aName, aSex, aWorkplace, aPhone, aCellphone, aAdd, aE, aBirth, aWorktime, aPosition, aEducation, aChinese, aEnglish,aImagepath);
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(insert, con);
try
{
cmd.ExecuteNonQuery();
}
catch
{
}
con.Close();
}
aImagepath始终取不到值。
自己解决了,用session会话解决 展开
1个回答
展开全部
上传组件中使用的input fileHTML控件,这个HTML控件有一种安全机智,不可使用非法上传客户端的文件信息而导致泄露隐私信息,而产生不可保留状态信息,所以在刷新后input file中的信息就会丢失。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询