非常不错的File文件控件,图片,flash,视频都可以立即显示,推荐使用。
我们平常用file文件控件上传文件,要预览有时会刷新页面,结果file控件被清空,好多人问能能让他不清空或重新赋值,因为处于安全性的考虑,这是不可能的。那怎么进行无刷新预览呢?这里我把我所使用的方法写一下。
选择页面:
复制代码 代码如下:
<script language="javascript">
function checkData()
{
var fileName=document.getElementById("FileUp").value;
if(fileName=="")
return;
//检查文件类型
var exName=fileName.substr(fileName.lastIndexOf(".")+1).toUpperCase()
if(exName=="JPG"||exName=="BMP"||exName=="GIF")
{
//document.getElementById("myimg").src=fileName;
document.getElementById("previewImage").innerHTML='<img src=https://www.jb51.net/article/\''+fileName+'https://www.jb51.net/article/\' width=100 height=100 >';
}
else
if(exName=="SWF")
{
document.getElementById("previewImage").innerHTML='<embed src=https://www.jb51.net/article/\''+fileName+'https://www.jb51.net/article/\' width=https://www.jb51.net/article/\'100\' height=https://www.jb51.net/article/\'100\' quality=https://www.jb51.net/article/\'high\' bgcolor=https://www.jb51.net/article/\'#f5f5f5\' ></embed>';
}
else
if(exName=="WMV"||exName=="MPEG"||exName=="ASF"||exName=="AVI")
{
var strcode='<embed src=https://www.jb51.net/article/\''+fileName+'https://www.jb51.net/article/\' border=https://www.jb51.net/article/\'0\' width=https://www.jb51.net/article/\'100\' height=https://www.jb51.net/article/\'100\' quality=https://www.jb51.net/article/\'high\' ';
strcode+=' autoStart=https://www.jb51.net/article/\'1\' playCount=https://www.jb51.net/article/\'0\' enableContextMenu=https://www.jb51.net/article/\'0\' type=https://www.jb51.net/article/\'application/x-mplayer2\'></embed>';
document.getElementById("previewImage").innerHTML=strcode;
}
else
{
alert("请选择正确的图片文件");
document.getElementById("FileUp").value="";
}
}
function openwin()
{
window.open("addPreview.aspx","","height=300,width=345,top=100,left=100");
}
</script>
HTML代码:
复制代码 代码如下:
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="middle">
<INPUT type="file" size="23"
runat="server" onchange="checkData()"><br>
注:这里可以是图片(jpg或gif格式),flash动画(swf)及视频文件(wmv,mpeg,asf,avi)。大小限定在1M以内。
</td>
<td>
<div>当前页预览</div>
</td>
</tr>
</table>
弹出预览页面:
复制代码 代码如下:
<script language="javascript">
function getstr()
{
var strcode="";
var width=100;
var high=100;
if(self.opener.document.getElementById("FileUp")!=null)
{
//strcode=self.opener.document.getElementById("previewImage").innerHTML;
width=self.opener.document.getElementById("lblWidth").innerText;
high=self.opener.document.getElementById("lblHigh").innerText;
var fileName=self.opener.document.getElementById("FileUp").value;
var exName=fileName.substr(fileName.lastIndexOf(".")+1).toUpperCase()
if(exName=="JPG"||exName=="BMP"||exName=="GIF")
{
//document.getElementById("myimg").src=fileName;
strcode='<img src=https://www.jb51.net/article/\''+fileName+'https://www.jb51.net/article/\' >';
}
else
if(exName=="SWF")
{
strcode='<embed src=https://www.jb51.net/article/\''+fileName+'https://www.jb51.net/article/\' width=https://www.jb51.net/article/\''+width+'https://www.jb51.net/article/\' height=https://www.jb51.net/article/\''+high+'https://www.jb51.net/article/\' quality=https://www.jb51.net/article/\'high\' ></embed>';
}
else
if(exName=="WMV"||exName=="MPEG"||exName=="ASF"||exName=="AVI")
{
strcode='<embed src=https://www.jb51.net/article/\''+fileName+'https://www.jb51.net/article/\' border=https://www.jb51.net/article/\'0\' width=https://www.jb51.net/article/\''+width+'https://www.jb51.net/article/\' height=https://www.jb51.net/article/\''+high+'https://www.jb51.net/article/\' quality=https://www.jb51.net/article/\'high\' ';
strcode+=' autoStart=https://www.jb51.net/article/\'1\' playCount=https://www.jb51.net/article/\'0\' enableContextMenu=https://www.jb51.net/article/\'0\' type=https://www.jb51.net/article/\'application/x-mplayer2\'></embed>';
}
}
if(self.opener.document.getElementById("txtADCode")!=null)
{
strcode=self.opener.document.getElementById("txtADCode").innerHTML;
}
if(strcode!="")
{
//window.alert(fileName);
document.getElementById("showimg").innerHTML=strcode;
}
}
</script>
显示:
复制代码 代码如下:
<div></div>