如何达到点击浏览选择图片后,立马显示预览图片?
string bookName = TextBox2.Text.Trim();
string bookLeibie = TextBox3.Text.Trim();
string picture = FileUpload1.FileName;
string lastName = picture.Substring(picture.LastIndexOf(".") + 1);
string[] name={"bmp","jpg","gif"};
SqlConnection SqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString);
SqlCon.Open();
string SavaPath = Server.MapPath("image/")+picture;
FileUpload1.PostedFile.SaveAs(SavaPath);
string sqlStr = "Insert into T_Book_Picture(ID,图书类别,图书名,图片) values(newid(),'" + bookLeibie + "','" + bookName + "','"+ picture + "')";
SqlCommand SqlCom = new SqlCommand(sqlStr, SqlCon);
SqlCom.ExecuteNonQuery();
SqlCon.Close();
Image1.Visible = true;
Image1.ImageUrl = SavaPath;
我是先把图片设为不可见,想点击浏览后,立即可以显示图片。
------解决方案--------------------
你要兼容行么?
JScript code
<html>
<head>
<title>get file input full path</title>
<script language='javascript'>
function getFullPath(obj)
{
if(obj)
{
//ie
if (window.navigator.userAgent.indexOf("MSIE")>=1)
{
obj.select();
return document.selection.createRange().text;
}
//firefox
else if(window.navigator.userAgent.indexOf("Firefox")>=1)
{
if(obj.files)
{
return obj.files.item(0).getAsDataURL();
}
return obj.value;
}
return obj.value;
}
}
</script>
</head>
<body>
<input type="file" onchange="document.getElementById('img').src=getFullPath(this);" />
<img id="img" height="174" width="234"/>
</body>
</html>