js實(shí)現(xiàn)圖片上傳并正常顯示
項(xiàng)目經(jīng)常會(huì)用到頭像上傳,那么怎么實(shí)現(xiàn)呢?
首先是HTML布局:
<label for="thumbnail" class="col-md-3 control-label">縮略圖</label> <div class="col-md-6"> <input type="file" class="form-control" id="thumbnail" name="thumbnail"> </div>
jquery方式,IE不支持,但I(xiàn)E會(huì)獲得絕對(duì)的上傳路徑信息:
function getObjectURL(file) {
var url = null ;
if (window.createObjectURL!=undefined) { // basic
url = window.createObjectURL(file) ;
} else if (window.URL!=undefined) { // mozilla(firefox)
url = window.URL.createObjectURL(file) ;
} else if (window.webkitURL!=undefined) { // webkit or chrome
url = window.webkitURL.createObjectURL(file) ;
}
return url ;
}
$('#thumbnail').change(function() {
var eImg = $('<img />');
eImg.attr('src', getObjectURL($(this)[0].files[0])); // 或 this.files[0] this->input
$(this).after(eImg);});

網(wǎng)上找一份可用的代碼非常不易,經(jīng)過不斷的篩選總結(jié)才得出兼容所有的文件上傳顯示
HTML布局
<form action='' method='post' name='myform'> <input type='file' id='iptfileupload' onchange='show()' value='' /> <img src='1.jpg' alt='' id='img' /> </form>
JS代碼:
<script type="text/javascript">
function getPath(obj,fileQuery,transImg) {
var imgSrc = '', imgArr = [], strSrc = '' ;
if(window.navigator.userAgent.indexOf("MSIE")>=1){ // IE瀏覽器判斷
if(obj.select){
obj.select();
var path=document.selection.createRange().text;
alert(path) ;
obj.removeAttribute("src");
imgSrc = fileQuery.value ;
imgArr = imgSrc.split('.') ;
strSrc = imgArr[imgArr.length - 1].toLowerCase() ;
if(strSrc.localeCompare('jpg') === 0 || strSrc.localeCompare('jpeg') === 0 || strSrc.localeCompare('gif') === 0 || strSrc.localeCompare('png') === 0){
obj.setAttribute("src",transImg);
obj.style.filter=
"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+path+"', sizingMethod='scale');"; // IE通過濾鏡的方式實(shí)現(xiàn)圖片顯示
}else{
//try{
throw new Error('File type Error! please image file upload..');
//}catch(e){
// alert('name: ' + e.name + 'message: ' + e.message) ;
//}
}
}else{
// alert(fileQuery.value) ;
imgSrc = fileQuery.value ;
imgArr = imgSrc.split('.') ;
strSrc = imgArr[imgArr.length - 1].toLowerCase() ;
if(strSrc.localeCompare('jpg') === 0 || strSrc.localeCompare('jpeg') === 0 || strSrc.localeCompare('gif') === 0 || strSrc.localeCompare('png') === 0){
obj.src = fileQuery.value ;
}else{
//try{
throw new Error('File type Error! please image file upload..') ;
//}catch(e){
// alert('name: ' + e.name + 'message: ' + e.message) ;
//}
}
}
} else{
var file =fileQuery.files[0];
var reader = new FileReader();
reader.onload = function(e){
imgSrc = fileQuery.value ;
imgArr = imgSrc.split('.') ;
strSrc = imgArr[imgArr.length - 1].toLowerCase() ;
if(strSrc.localeCompare('jpg') === 0 || strSrc.localeCompare('jpeg') === 0 || strSrc.localeCompare('gif') === 0 || strSrc.localeCompare('png') === 0){
obj.setAttribute("src", e.target.result) ;
}else{
//try{
throw new Error('File type Error! please image file upload..') ;
//}catch(e){
// alert('name: ' + e.name + 'message: ' + e.message) ;
//}
}
// alert(e.target.result);
}
reader.readAsDataURL(file);
}
}
function show(){
//以下即為完整客戶端路徑
var file_img=document.getElementById("img"),
iptfileupload = document.getElementById('iptfileupload') ;
getPath(file_img,iptfileupload,file_img) ;
}
</script>
更多精彩內(nèi)容請(qǐng)參考專題《ajax上傳技術(shù)匯總》,《javascript文件上傳操作匯總》和《jQuery上傳操作匯總》進(jìn)行學(xué)習(xí)。
希望本文所述對(duì)大家學(xué)習(xí)javascript程序設(shè)計(jì)有所幫助。
- js 實(shí)現(xiàn) input type="file" 文件上傳示例代碼
- JS實(shí)現(xiàn)上傳圖片的三種方法并實(shí)現(xiàn)預(yù)覽圖片功能
- js實(shí)現(xiàn)圖片上傳并預(yù)覽功能
- 簡(jiǎn)單實(shí)現(xiàn)js上傳文件功能
- Servlet+Jsp實(shí)現(xiàn)圖片或文件的上傳功能具體思路及代碼
- js實(shí)現(xiàn)上傳圖片預(yù)覽的方法
- JS中使用FormData上傳文件、圖片的方法
- js獲取上傳文件的絕對(duì)路徑實(shí)現(xiàn)方法
- 上傳圖片預(yù)覽JS腳本 Input file圖片預(yù)覽的實(shí)現(xiàn)示例
- JavaScript實(shí)現(xiàn)大文件分片上傳處理
相關(guān)文章
Bootstrap幻燈片輪播圖支持觸屏左右手勢(shì)滑動(dòng)的實(shí)現(xiàn)方法
最近在研究用bootstrap搭建網(wǎng)站,Bootstrap能自適應(yīng)pc端和手機(jī)端,并且移動(dòng)設(shè)備優(yōu)先,適合現(xiàn)如今移動(dòng)營銷,大家用的設(shè)備基本是觸屏的了,能用滑動(dòng)交互在小屏幕上體驗(yàn)會(huì)更好,那么如何實(shí)現(xiàn)呢?下面小編給大家介紹下bootstrap 手勢(shì)滑動(dòng)輪播圖的實(shí)現(xiàn)方法2016-10-10
解決微信小程序中轉(zhuǎn)換時(shí)間格式IOS不兼容的問題
今天小編就為大家分享一篇關(guān)于解決微信小程序中轉(zhuǎn)換時(shí)間格式IOS不兼容的問題,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02
setInterval計(jì)時(shí)器不準(zhǔn)的問題解決方法
在js中如果打算使用setInterval進(jìn)行倒數(shù),計(jì)時(shí)等功能,往往是不準(zhǔn)確的,針對(duì)這個(gè)問題,本文有個(gè)不錯(cuò)的解決方案2014-05-05
詳解基于Vue cli生成的Vue項(xiàng)目的webpack4升級(jí)
這篇文章主要介紹了詳解基于Vue cli生成的Vue項(xiàng)目的webpack4升級(jí),本文將詳細(xì)介紹從webpack3到webpack4的升級(jí)過程,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06

