Home  >  Article  >  Web Front-end  >  How to convert images to base64 in uniapp

How to convert images to base64 in uniapp

coldplay.xixi
coldplay.xixiOriginal
2020-12-08 14:54:3017474browse

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
            }

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!

Statement:
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