JavsScrpt實(shí)現(xiàn)上傳圖片預(yù)覽功能,兼容各大瀏覽器
來源:易賢網(wǎng) 閱讀:1020 次 日期:2015-04-17 11:04:25
溫馨提示:易賢網(wǎng)小編為您整理了“JavsScrpt實(shí)現(xiàn)上傳圖片預(yù)覽功能,兼容各大瀏覽器”,方便廣大網(wǎng)友查閱!

本上傳功能的實(shí)現(xiàn)采用iframe,這里主要寫的是實(shí)現(xiàn)圖片未上傳時(shí)的實(shí)時(shí)預(yù)覽功能,即在未上傳圖片前可看到該選擇圖片。兼容IE6\7\8\9、chrome、opera、firefox,不兼容safari,IE9以上版本未測(cè)試。

代碼片段一:

<script language="javascript">

function InitImageObj(fileObj, p_doma, p_domg, p_domd){

var allowExtention = ".jpg,.jpeg,.bmp,.gif,.png";

var doma = parent.document.getElementById(p_doma);

var domg = parent.document.getElementById(p_domg);

var domd = parent.document.getElementById(p_domd);

var extention = fileObj.value.substring(fileObj.value.lastIndexOf(".")+1).toLowerCase();

var browserVersion = window.navigator.userAgent.toUpperCase();

if(allowExtention.indexOf(extention)>-1){

if (browserVersion.indexOf("MSIE")>-1){

domd.innerHTML = "";

fileObj.select();

domd.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";

domd.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = fileObj.value;

doma.href = fileObj.value;

}else if(browserVersion.indexOf("FIREFOX")>-1){

var firefoxVersion = parseFloat(browserVersion.toLowerCase().match(/firefox\/([\d.]+)/)[1]);

if(firefoxVersion<7){

doma.href = fileObj.files[0].getAsDataURL();

domg.src = fileObj.files[0].getAsDataURL();

}else{

doma.href = window.URL.createObjectURL(fileObj.files[0]);

domg.src = window.URL.createObjectURL(fileObj.files[0]);

}

}else if(fileObj.files){

if(typeof FileReader !== "undefined"){

var reader = new FileReader();

reader.onload = function(e){

doma.href = e.target.result;

domg.src = e.target.result;

}

reader.readAsDataURL(fileObj.files[0]);

}else if(browserVersion.indexOf("SAFARI")>-1){

alert("圖片預(yù)覽暫時(shí)不支持safari瀏覽器!");

return;

}

}else{

alert("你的瀏覽器不支持圖片預(yù)覽!");

return;

}

}else{

fileObj.value="";

if(browserVersion.indexOf("MSIE")>-1){

fileObj.select();

document.selection.clear();

}

fileObj.outerHTML=fileObj.outerHTML;

alert("僅支持"+allowExtention+"為后綴名的文件!");

return;

}

}

</script>

代碼片段二(CSS):

.imgfix{padding-left:0; [;padding-left:2px;]; _padding-left:0;}

.iframefix{_padding-left:0px;}

.fontfix{font-weight: normal !important;}

.ie6fix{display:inline-block; _zoom:1; _display:inline;}

.imgdiv{display:inline-block; [;padding-left:2px;];}

代碼片段三:

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf8">

<script language="javascript">

function InitImageObj(fileObj, p_doma, p_domg, p_domd){

var allowExtention = ".jpg,.jpeg,.bmp,.gif,.png";

var doma = parent.document.getElementById(p_doma);

var domg = parent.document.getElementById(p_domg);

var domd = parent.document.getElementById(p_domd);

var extention = fileObj.value.substring(fileObj.value.lastIndexOf(".")+1).toLowerCase();

var browserVersion = window.navigator.userAgent.toUpperCase();

if(allowExtention.indexOf(extention)>-1){

if (browserVersion.indexOf("MSIE")>-1){

domd.innerHTML = "";

fileObj.select();

domd.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";

domd.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = fileObj.value;

doma.href = fileObj.value;

}else if(browserVersion.indexOf("FIREFOX")>-1){

var firefoxVersion = parseFloat(browserVersion.toLowerCase().match(/firefox\/([\d.]+)/)[1]);

if(firefoxVersion<7){

doma.href = fileObj.files[0].getAsDataURL();

domg.src = fileObj.files[0].getAsDataURL();

}else{

doma.href = window.URL.createObjectURL(fileObj.files[0]);

domg.src = window.URL.createObjectURL(fileObj.files[0]);

}

}else if(fileObj.files){

if(typeof FileReader !== "undefined"){

var reader = new FileReader();

reader.onload = function(e){

doma.href = e.target.result;

domg.src = e.target.result;

}

reader.readAsDataURL(fileObj.files[0]);

}else if(browserVersion.indexOf("SAFARI")>-1){

alert("圖片預(yù)覽暫時(shí)不支持safari瀏覽器!");

return;

}

}else{

alert("你的瀏覽器不支持圖片預(yù)覽!");

return;

}

}else{

fileObj.value="";

if(browserVersion.indexOf("MSIE")>-1){

fileObj.select();

document.selection.clear();

}

fileObj.outerHTML=fileObj.outerHTML;

alert("僅支持"+allowExtention+"為后綴名的文件!");

return;

}

}

</script>

</head>

<style>

td{font-size:12px;}

</style>

<body bgcolor="#EFF3FF" leftmargin="0" topmargin="0">

<table cellpadding="0" cellspacing="0" border="0" width="480px" height="25px">

<form name="form1" enctype="multipart/form-data" method="post" action="upPicPost.php" onsubmit="if (document.all('fileimage').value==''){alert('請(qǐng)選擇圖片');return false;}else{this.submitUpload.disabled=true;return true;}">

<tr>

<td>

<input type="file" name="fileimage" onchange="InitImageObj(this,'uploadpic_a','uploadpic_g', 'uploadpic_d');">

<input class="button1" type="submit" name="submitUpload" value="上傳">

</td>

</tr>

</form>

</table>

</html>

代碼片段四:

<td>

<input type="text" name="imgs" id="uploadpic_i" size="50" value="" />

<br />

<a id="uploadpic_a" href="/def.jpg" target="_blank">

<div id="uploadpic_d" class="imgdiv" style="width:110px; height:80px;">

<img id="uploadpic_g" src="/def.jpg" border="0" target="blank" width="110px" height="80px" />

</div>

</a>

<iframe id="UploadFiles" src="/UpPic.php" frameborder="0" scrolling="no" width="600px" height="25px"></iframe>

</td>

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看腳本欄目
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)