vue验证码图片显示不出来
后台直接返回的是一张图片,直接把请求的url放在img的src里面是可以显示图片的,但是我的电脑就是显示不出来。于是就想将图片转为base64格式。
上代码
封装的api
export const fun_GetImgCode = (url, params) => {
return axios({
url: `${url}`,
responseType: "arraybuffer", // 类型
method: "get",
params
});
};
引入api
import { fun_GetImgCode } from "@/api";
调用api
fun_GetImgCode(url, params).then(res => {
const { status, data } = res;
if (status === 200) {
const img = btoa(
new Uint8Array(data).reduce(
(data, byte) => data + String.fromCharCode(byte), "")
);
this.securitySrc.src = "data:image/png;base64," + img;
}
})
在Vue项目中遇到一个问题,后台直接返回的验证码图片无法正常显示。尝试将图片转换为Base64格式解决,通过axios发送get请求,设置responseType为'arraybuffer',然后将返回的二进制数据转换为Base64字符串,最后设置img标签的src属性。代码已给出详细实现过程。
1087





