上传图片生成base64
// 图片生成base64格式
function handleFileChange(event) {
const file = event.target.files[0]
if (file) {
const reader = new FileReader()
reader.onload = (e) => {
imageBase64.value = e.target.result
}
reader.readAsDataURL(file)
}
}
async function uploadImage() {
if (imageBase64.value) {
const param = {
url: imageBase64.value,
path: imageBase64.value,
name: imageBase64.value,
file: imageBase64.value,
}
apiUserlist.uploadimg(param).then((res: any) => {
console.log('uploadimg', res.data)
})
}
}
<input type="file" @change="handleFileChange"> <button @click="uploadImage"> 上传图片 </button>