base64转file
export function base64toFile (dataurl, filename) {
let arr = dataurl.split(',')
let mime = arr[0].match(/:(.*?);/)[1]
let suffix = mime.split('/')[1]
let bstr = atob(arr[1])
let n = bstr.length
let u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new File([u8arr], `${filename}.${suffix}`, {
type: mime
})
}
file,Blob转base64
export const getBase64 = (img:any, callback: (url: string) => void) => {
const reader = new FileReader();
reader.addEventListener('load', () => callback(reader.result as string));
reader.readAsDataURL(img as Blob);
};
getBase64(img, (url) => {
console.log(url,'生成的base64')
});
记录请求file文件通过二进制流得到云图片
export const formData = (data: any) => {
const formData = new FormData()
const entries = Object.entries(data)
entries.forEach((item) => {
if (item[1] && item[1] instanceof File) {
formData.append(item[0], item[1])
} else {
const value =
isObject(item[1]) || Array.isArray(item[1]) ? JSON.stringify(item[1]) : item[1]
formData.append(item[0], value as string | Blob)
}
})
return formData
}
return request('/image_translate/image_to_url', {
method: 'POST',
data: formData({file: url}),
headers: {
'Content-Type':
'multipart/form-data; boundary=----WebKitFormBoundarynl6gT1BKdPWIejNq',
},
});
记录前端获取oss图片转为base64
export async function getBlobTransitionBase64(url) {
return request(url, {
method: 'GET',
headers: {
'Cache-Control': 'no-cache',
},
responseType: 'blob',
});
}