Home > Web Front-end > uni-app > body text

How to convert images to base64 in uniapp

coldplay.xixi
Release: 2023-01-13 00:44:08
Original
17357 people have browsed it

Uniapp's method of converting images to base64: first select the relative path returned by the image; then set the encoding format, the code is [filePath: url,encoding: 'base64',success: res => {console.log (res);].

How to convert images to base64 in uniapp

The operating environment of this tutorial: windows7 system, uni-app2.5.1 version, Dell G3 computer. This method is suitable for all brands of computers.

Uniapp's method of converting pictures to base64:

Convert pictures to base64

            urlTobase64(url){
                // #ifdef MP-WEIXIN
                    uni.getFileSystemManager().readFile({
                        filePath: url, //选择图片返回的相对路径
                        encoding: 'base64', //编码格式
                        success: res => { //成功的回调
                        console.log(res);
                            let base64 = 'data:image/jpeg;base64,' + res.data //不加上这串字符,在页面无法显示的哦
                        },fail: (e) => {
                            console.log("图片转换失败");
                        }
                    })
                // #endif
                
                // #ifndef MP-WEIXIN
                    uni.request({
                        url: url,
                        method:'GET',
                        responseType:'arraybuffer',
                        success: ress => {
                            let base64 = wx.arrayBufferToBase64(ress.data); //把arraybuffer转成base64 
                            base64 = 'data:image/jpeg;base64,' + base64 //不加上这串字符,在页面无法显示的哦
                        },fail: (e) => {
                            console.log("图片转换失败");
                        }
                    })
                // #endif
            }
Copy after login

Related free learning recommendations: Programming Video

Recommended (free): uni-app development tutorial

The above is the detailed content of How to convert images to base64 in uniapp. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!