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(res).reduce(
(data, byte) => data + String.fromCharCode(byte), "")
);
this.securitySrc.src = "data:image/png;base64," + img;
}
})
絕對手打,覺得有用的素質三連,謝謝!