封装对应的js文件
function base64toFile(data, fileName) {
const dataArr = data.split(',')
const byteString = atob(dataArr[1])
const options = {
type: 'image/jpeg',
endings: 'native',
}
const u8Arr = new Uint8Array(byteString.length)
for (let i = 0; i < byteString.length; i++) {
u8Arr[i] = byteString.charCodeAt(i)
}
return new File([u8Arr], `${fileName}.jpg`, options)
}
export default base64toFile
再到对应的页面引入此文件
import base64toFile from '@/utils'
const imgSrc = `data:;base64,${res}`
const str = base64toFile(imgSrc, 'file')