


How to convert vue.js images to Base64, upload images and preview them
This article introduces you to how to convert vue.js images to Base64 to upload images and preview them. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
For front-end personnel, image processing is a very common requirement. Since the image is slightly special, most methods now use the ajax interface to submit it through the http method, such as the post method to submit, and return after background processing. An image path is given to the front end, and the front end writes the img tag according to this path. However, based on the current development model of separation of front and rear ends, the front and rear end codes are often not in the same system directory, and the liunx path and the windows path may be different during deployment. Such later path changes may cause maintenance difficulties.
To address this problem, here I recommend converting the image to base64 format and then sending it to the backend. The backend only needs to store the transcoding results in the database, and the frontend calls the interface to directly obtain the base64 data and write it directly. Enter the img src tag
The following uses the element ui upload component to implement the idea
The code is as follows:
<el-upload ref='upload' :auto-upload='false' :file-list="fileList" :multiple='false' :limit="1" :on-exceed="handleExceed" :http-request="uploadFiles" accept="image/jpeg,image/gif,image/png" action='' :on-change='changeUpload' > <el-button slot="trigger" size="mini" type="primary">选取图片</el-button> <span> </span> <el-button @click='uploadFiles' size="mini" type="primary">点击上传</el-button> </el-upload>
js part
//点击上传图片,上传成功返回图片路径 uploadFiles(){ var That=this; let file=this.$refs.upload.$refs['upload-inner'].$refs.input; //获取文件数据 let fileList=file.files; var imgFile; let reader = new FileReader(); //html5读文件 reader.readAsDataURL(fileList[0]); //转BASE64 reader.onload=function(e) { //读取完毕后调用接口 imgFile = e.target.result; let obj={ id: "loginLogo", configGroup: "logo", configItem : "loginLogo", itemValue : imgFile } return BaseApi.uploadFiles(obj).then((res)=>{ if(res.status=='SUCCESS'){ AlertBox('图片上传成功!','success',true).then(()=>{ return That.getSysLogo(); //调用获取base64数据接口 }); }else{ Alert('图片上传失败',res); return '' } }) }; },
Finally, in the interface img src Just bind the base64 string returned by the That.getSysLogo() interface to the label!
Recommended related articles:
How to define global variables and global methods in vue? (Code)
The above is the detailed content of How to convert vue.js images to Base64, upload images and preview them. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
