jquery在Firefox瀏覽中上傳圖片並顯示

來源:互聯網
上載者:User

標籤:

由於Firefox瀏覽器有很多代碼無法相容,特別是圖片的顯示,下面我介紹一種是我親自測試過可以實現的方法

<script>

function setImagePreview() {

//var ix = preview.width.value

//var iy = form.height.value;

var docObj=document.getElementById("doc");

var imgObjPreview=document.getElementById("preview");

 

if(docObj.files && docObj.files[0]){

//Firefox下,直接設img屬性

imgObjPreview.style.display = ‘block‘;

imgObjPreview.style.width = ‘300px‘;

imgObjPreview.style.height = ‘120px‘;

//Firefox7以上版本不能用上面的getAsDataURL()方式擷取,需要以下方式

imgObjPreview.src = window.URL.createObjectURL(docObj.files[0]);

}else{

//IE下,使用濾鏡

docObj.select();

var imgSrc = document.selection.createRange().text;

var localImagId = document.getElementById("localImag");

//必須設定初始大小

localImagId.style.width = "250px";

localImagId.style.height = "200px";

//圖片異常的捕捉,防止使用者修改尾碼來偽造圖片

try{

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

localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc;

}catch(e){

alert("您上傳的圖片格式不正確,請重新選擇!");

return false;

}

imgObjPreview.style.display = ‘none‘;

document.selection.empty();

}

return true;

}

</script>

 

<form name=imageform>

   <input type=file name="doc" id="doc" onchange="javascript:setImagePreview();"/>

<p><div id="localImag"><img id="preview" width=-1 height=-1 style="diplay:none" /></div></p>

</form>

jquery在Firefox瀏覽中上傳圖片並顯示

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.