今天看見網(wǎng)上各位大佬五花八門的前端圖片獲取并轉(zhuǎn)化為base64格式的操作,果然高手在民間,很神奇,沒想到有這么多方法,那我也來寫一個我自己的方法
我是習(xí)慣使用filereader這個內(nèi)置類,感覺比較簡單,不用管內(nèi)部原理,反正可以獲取圖片,實現(xiàn)在互聯(lián)網(wǎng)上的傳輸就可以啦!
首先,我們需要使用input(file類型)和一個button元素,一個負(fù)責(zé)上傳圖片,一個負(fù)責(zé)處理圖片,在處理函數(shù)里面,通過files和readasdataurl方法配合,實現(xiàn)圖片的轉(zhuǎn)化:
元素
<div class="mybox">
<input type="file" name="" id="sendimg">
<button onclick="handleimg()">處理</button>
</div>
函數(shù)文章來源:http://www.zghlxwxcb.cn/news/detail-698598.html
function handleimg()
{
console.log('點擊了');
var img=document.getElementById('sendimg')
img=img.files
const reader=new FileReader()
reader.readAsDataURL(img[0])
reader.onload=function()
{
console.log(this.result);
}
}
上述的this.result就是base64結(jié)果
今天的分享就到這里??!文章來源地址http://www.zghlxwxcb.cn/news/detail-698598.html
到了這里,關(guān)于前端獲取本地圖片并且轉(zhuǎn)化為base64格式的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!