File文件控件,选中文件(图片,flash,视频)即立即预览显示

本文介绍了一种在用户选择文件后即时预览文件内容的方法,适用于多种文件格式,包括图片、Flash和视频等,并提供了具体的JavaScript代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


None.gif   继续总结项目里的一些小TIP,我们平常用file文件控件上传文件,要预览有时会刷新页面,结果file控件被清空,好多人问能能让他不清空或重新赋值,因为处于安全性的考虑,这是不可能的。那怎么进行无刷新预览呢?这里我把我所使用的方法写一下。 
None.gif选择页面:
None.gif
ExpandedBlockStart.gif
<script language="javascript">
InBlock.gif
function checkData()
ExpandedSubBlockStart.gif
{
InBlock.gif  
var fileName=document.getElementById("FileUp").value;
InBlock.gif  
if(fileName=="")
InBlock.gif   
return;
InBlock.gif  
//检查文件类型
InBlock.gif
  var exName=fileName.substr(fileName.lastIndexOf(".")+1).toUpperCase()    
InBlock.gif  
if(exName=="JPG"||exName=="BMP"||exName=="GIF")
ExpandedSubBlockStart.gif  
{
InBlock.gif   
//document.getElementById("myimg").src=fileName;
InBlock.gif
   document.getElementById("previewImage").innerHTML='<img src=\''+fileName+'\' width=100 height=100 >';
ExpandedSubBlockEnd.gif  }

InBlock.gif  
else
InBlock.gif  
if(exName=="SWF")
ExpandedSubBlockStart.gif  
{
InBlock.gif   document.getElementById(
"previewImage").innerHTML='<embed src=\''+fileName+'\' width=\'100\' height=\'100\' quality=\'high\' bgcolor=\'#f5f5f5\' ></embed>';     
ExpandedSubBlockEnd.gif  }

InBlock.gif  
else
InBlock.gif  
if(exName=="WMV"||exName=="MPEG"||exName=="ASF"||exName=="AVI")
ExpandedSubBlockStart.gif  
{
InBlock.gif   
var strcode='<embed src=\''+fileName+'\' border=\'0\' width=\'100\' height=\'100\' quality=\'high\' ';
InBlock.gif   strcode
+=' autoStart=\'1\' playCount=\'0\' enableContextMenu=\'0\' type=\'application/x-mplayer2\'></embed>';
InBlock.gif   document.getElementById(
"previewImage").innerHTML=strcode;
ExpandedSubBlockEnd.gif  }

InBlock.gif  
else    
ExpandedSubBlockStart.gif  
{
InBlock.gif   alert(
"请选择正确的图片文件");
InBlock.gif   document.getElementById(
"FileUp").value="";
ExpandedSubBlockEnd.gif  }
 
ExpandedSubBlockEnd.gif}

InBlock.gif
function openwin() 
ExpandedSubBlockStart.gif
{    
InBlock.gif window.open(
"addPreview.aspx","","height=300,width=345,top=100,left=100");  
ExpandedSubBlockEnd.gif}
 
ExpandedBlockEnd.gif
None.gif
</script>
None.gif
None.gifHTML代码:
None.gif
None.gif
<table border="0" cellpadding="0" cellspacing="0" width="100%" height="100%" ID="Table1">
None.gif 
<tr>
None.gif  
<td width="255" height="100%" valign="middle">
None.gif   
<INPUT id="FileUp" style="WIDTH: 253px; HEIGHT: 22px" type="file" size="23" name="File1"
None.gif    runat
="server" onchange="checkData()"><br>
None.gif   
&nbsp;&nbsp;注:这里可以是图片(jpg或gif格式),flash动画(swf)及视频文件(wmv,mpeg,asf,avi)。大小限定在1M以内。
None.gif  
</td>
None.gif  
<td>
None.gif   
<div id="previewImage">当前页预览</div>
None.gif  
</td>
None.gif 
</tr>
None.gif
</table>
None.gif
None.gif弹出预览页面:
None.gif
ExpandedBlockStart.gif
<script language="javascript">
InBlock.gif
function getstr()
ExpandedSubBlockStart.gif
{   
InBlock.gif 
var strcode=""
InBlock.gif 
var width=100;
InBlock.gif 
var high=100;
InBlock.gif 
if(self.opener.document.getElementById("FileUp")!=null)
ExpandedSubBlockStart.gif 
{
InBlock.gif  
//strcode=self.opener.document.getElementById("previewImage").innerHTML;
InBlock.gif
  width=self.opener.document.getElementById("lblWidth").innerText;
InBlock.gif  high
=self.opener.document.getElementById("lblHigh").innerText;
InBlock.gif  
InBlock.gif  
var fileName=self.opener.document.getElementById("FileUp").value;
InBlock.gif  
var exName=fileName.substr(fileName.lastIndexOf(".")+1).toUpperCase()    
InBlock.gif  
if(exName=="JPG"||exName=="BMP"||exName=="GIF")
ExpandedSubBlockStart.gif  
{
InBlock.gif   
//document.getElementById("myimg").src=fileName;
InBlock.gif
   strcode='<img src=\''+fileName+'\' width='+width+' height='+high+>';
ExpandedSubBlockEnd.gif  }

InBlock.gif  
else
InBlock.gif  
if(exName=="SWF")
ExpandedSubBlockStart.gif  
{
InBlock.gif   strcode
='<embed src=\''+fileName+'\' width=\''+width+'\' height=\''+high+'\' quality=\'high\' ></embed>';     
ExpandedSubBlockEnd.gif  }

InBlock.gif  
else
InBlock.gif  
if(exName=="WMV"||exName=="MPEG"||exName=="ASF"||exName=="AVI")
ExpandedSubBlockStart.gif  
{
InBlock.gif  strcode
='<embed src=\''+fileName+'\' border=\'0\' width=\''+width+'\' height=\''+high+'\' quality=\'high\' ';
InBlock.gif  strcode
+=' autoStart=\'1\' playCount=\'0\' enableContextMenu=\'0\' type=\'application/x-mplayer2\'></embed>';
ExpandedSubBlockEnd.gif  }

InBlock.gif  
ExpandedSubBlockEnd.gif }
    
InBlock.gif 
if(self.opener.document.getElementById("txtADCode")!=null)
ExpandedSubBlockStart.gif 
{
InBlock.gif  strcode
=self.opener.document.getElementById("txtADCode").innerHTML; 
ExpandedSubBlockEnd.gif }
    
InBlock.gif 
if(strcode!="")
ExpandedSubBlockStart.gif 
{
InBlock.gif  
//window.alert(fileName);
InBlock.gif
  document.getElementById("showimg").innerHTML=strcode;
ExpandedSubBlockEnd.gif }
   
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif
None.gif
</script>
None.gif
None.gif显示:
None.gif
None.gif
<div id="showimg"></div>
None.gif
None.gif
None.gif
None.gif上传图片时实现即时预览
None.gif解决方法:
ExpandedBlockStart.gif
<script language="javascript">
InBlock.gif        
function showimg()
ExpandedSubBlockStart.gif        
{
InBlock.gif                  document.all(
"image1").src=document.all("upfile").value;
ExpandedSubBlockEnd.gif         }

ExpandedBlockEnd.gif
None.gif
</script>
None.gif
None.gif…… ……
None.gif
<input type="file" id="upfile" name="upfile" runat="server" onpropertychange="showimg()">
None.gif
<asp:Image id="Image1" style="…… ……" runat="server"></asp:Image>
None.gif
None.gif



本文转自高海东博客园博客,原文链接:http://www.cnblogs.com/ghd258/archive/2005/12/03/290031.html,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值