当前位置:网站首页>Encapsulate the method of converting a picture file object to Base64
Encapsulate the method of converting a picture file object to Base64
2022-06-24 12:34:00 【Deshun】
frequently-used UI Component libraries such as Ant Design 、 Element UI There will be Upload Components , Support change Other events , But the event returns a file object , If you need a local live preview , It needs to be converted into Base64 Format .
You can encapsulate a method , Convenient for later use .
@/utils/getBase64.js
/**
* Create by Deshun
* E-mail: [email protected]/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-yjshash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-yjsemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */
* Date: 2021-05-27 Afternoon 8:07:12
*/
// File object transfer base64
export default function getBase64 (obj) {
const image = obj; // Get the selected picture in the file field
let reader = new FileReader(); // Instantiate the file to read the object
reader.readAsDataURL(image); // Read file as DataURL, That is to say base64 code
reader.onload = function (ev) { // Triggered when the file read completes successfully
let dataURL = ev.target.result; // After the file is read successfully DataURL, That is to say base64 code
return dataURL
}
}Introduce... On the required page :
page.vue
<script>
// Reference method
import getBase64 from '@/utils/getBase64'
export default {
data() {
return {
...
}
},
methods: {
// Image file object transfer Base64
getBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result);
reader.onerror = error => reject(error);
})
},
// Upload & Select Picture
handleChange(file) {
// Read & Processing images
this.getBase64(file).then(res => {
let img = new Image()
img.src = res
img.onload = function () {
...
}
})
}
}
}
</script>No reprint without permission :w3h5 » Encapsulate a picture file object to Base64 Methods
边栏推荐
- How to make Baidu quickly include its own personal website?
- 5 points + single gene pan cancer pure Shengxin idea!
- Install MySQL in docker and modify my CNF profile
- Deep learning ~11+ a new perspective on disease-related miRNA research
- [mysql_16] variables, process control and cursors
- pipeline groovy
- Coinbase will launch the first encrypted derivative product for retail traders
- Discussion on redis communication protocol
- [Tencent cloud 618 countdown!] Promotion strategy of the promotion activities
- VaR in PHP_ export、print_ r、var_ Differences in dump debugging
猜你喜欢

Ten thousand campus developers play AI in a fancy way. It's enough to see this picture!

FreeRTOS overview and experience

使用开源工具 k8tz 优雅设置 Kubernetes Pod 时区

Opencv learning notes - loading and saving images
Deep parsing and implementation of redis pub/sub publish subscribe mode message queue
[mysql_16] variables, process control and cursors

Opencv learning notes - Discrete Fourier transform

ArrayList # sublist these four holes, you get caught accidentally

MySQL 外键影响

Group planning - General Review
随机推荐
How to write controller layer code gracefully?
Cloud native database: the outlet of the database, you can also take off
Introduction to C language circular statements (foe, while, do... While)
Example of SMS interface verification code function implemented by ThinkPHP framework
Deep learning ~11+ a new perspective on disease-related miRNA research
A flaw in R markdown: folders cannot have Chinese
Identification of new prognostic DNA methylation features in uveal melanoma by 11+ based on methylation group and transcriptome analysis~
QT -- the qtabwidget supports dragging tabbar items
The idea of "6 points + gene family" without experiment~
Istio FAQ: istio init crash
Making daily menu applet with micro build low code
不用做实验的6分+基因家族纯生信思路~
Speculation London gold short-term stable money making skills? Where is it safe to fry London gold?
怎样打新债具体操作 开户是安全的吗
哪个商业保险养老险好?2022年商业养老保险产品排名
炒伦敦金短线稳定赚钱技巧?在哪里炒伦敦金安全靠谱?
11+! Methylation modification patterns based on m6A regulatory factors in colon cancer are characterized by different tumor microenvironment immune spectra
使用开源工具 k8tz 优雅设置 Kubernetes Pod 时区
Popular science of data annotation: ten common image annotation methods
Do you really know "open source"? Please check [nanny level] open source Encyclopedia