File文件控件,选中文件(图片,flash,视频)即立即预览显示
继续总结项目里的一些小TIP,我们平常用file文件控件上传文件,要预览有时会刷新页面,结果file控件被清空,好多人问能能让他不清空或重新赋值,因为处于安全性的考虑,这是不可能的。那怎么进行无刷新预览呢?这里我把我所使用的方法写一下。
选择页面:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script language="javascript">![](https://www.cnblogs.com/Images/dot.gif)
function checkData()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
var fileName=document.getElementById("FileUp").value;
if(fileName=="")
return;
//检查文件类型
var exName=fileName.substr(fileName.lastIndexOf(".")+1).toUpperCase()
if(exName=="JPG"||exName=="BMP"||exName=="GIF")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//document.getElementById("myimg").src=fileName;
document.getElementById("previewImage").innerHTML='<img src=\''+fileName+'\' width=100 height=100 >';
}
else
if(exName=="SWF")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
document.getElementById("previewImage").innerHTML='<embed src=\''+fileName+'\' width=\'100\' height=\'100\' quality=\'high\' bgcolor=\'#f5f5f5\' ></embed>';
}
else
if(exName=="WMV"||exName=="MPEG"||exName=="ASF"||exName=="AVI")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
var strcode='<embed src=\''+fileName+'\' border=\'0\' width=\'100\' height=\'100\' quality=\'high\' ';
strcode+=' autoStart=\'1\' playCount=\'0\' enableContextMenu=\'0\' type=\'application/x-mplayer2\'></embed>';
document.getElementById("previewImage").innerHTML=strcode;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
alert("请选择正确的图片文件");
document.getElementById("FileUp").value="";
}
}
function openwin()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
window.open("addPreview.aspx","","height=300,width=345,top=100,left=100");
}
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
</script>
![](/Images/OutliningIndicators/None.gif)
HTML代码:
![](/Images/OutliningIndicators/None.gif)
<table border="0" cellpadding="0" cellspacing="0" width="100%" height="100%" ID="Table1">
<tr>
<td width="255" height="100%" valign="middle">
<INPUT id="FileUp" style="WIDTH: 253px; HEIGHT: 22px" type="file" size="23" name="File1"
runat="server" onchange="checkData()"><br>
注:这里可以是图片(jpg或gif格式),flash动画(swf)及视频文件(wmv,mpeg,asf,avi)。大小限定在1M以内。
</td>
<td>
<div id="previewImage">当前页预览</div>
</td>
</tr>
</table>
![](/Images/OutliningIndicators/None.gif)
弹出预览页面:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script language="javascript">![](https://www.cnblogs.com/Images/dot.gif)
function getstr()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
var strcode="";
var width=100;
var high=100;
if(self.opener.document.getElementById("FileUp")!=null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//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")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//document.getElementById("myimg").src=fileName;
strcode='<img src=\''+fileName+'\' width='+width+' height='+high+' >';
}
else
if(exName=="SWF")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strcode='<embed src=\''+fileName+'\' width=\''+width+'\' height=\''+high+'\' quality=\'high\' ></embed>';
}
else
if(exName=="WMV"||exName=="MPEG"||exName=="ASF"||exName=="AVI")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strcode='<embed src=\''+fileName+'\' border=\'0\' width=\''+width+'\' height=\''+high+'\' quality=\'high\' ';
strcode+=' autoStart=\'1\' playCount=\'0\' enableContextMenu=\'0\' type=\'application/x-mplayer2\'></embed>';
}
}
if(self.opener.document.getElementById("txtADCode")!=null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strcode=self.opener.document.getElementById("txtADCode").innerHTML;
}
if(strcode!="")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//window.alert(fileName);
document.getElementById("showimg").innerHTML=strcode;
}
}
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
</script>
![](/Images/OutliningIndicators/None.gif)
显示:
![](/Images/OutliningIndicators/None.gif)
<div id="showimg"></div>
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
上传图片时实现即时预览
解决方法:
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script language="javascript">![](https://www.cnblogs.com/Images/dot.gif)
function showimg()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
document.all("image1").src=document.all("upfile").value;
}
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
</script>
![](/Images/OutliningIndicators/None.gif)
…… ……
<input type="file" id="upfile" name="upfile" runat="server" onpropertychange="showimg()">
<asp:Image id="Image1" style="…… ……" runat="server"></asp:Image>
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
原文地址:https://www.cnblogs.com/ghd258/p/290031.html