// base64转文件流
base64toFile(dataurl, filename = new Date().valueOf()) {
const arr = dataurl.split(',')
const mime = arr[0].match(/:(.*?);/)[1]
const suffix = mime.split('/')[1]
const bstr = atob(arr[1])
let n = bstr.length
const u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new File([u8arr], `${filename}.${suffix}`, {
type: mime
})
}
// 注意:上传图片要用post请求,并且将header设置为 'Content-Type': 'multipart/form-data'
async excuteUploadImage() {
const fileField = `data:image/png;base64,${photoStr}`
const form = new FormData()
form.append('file', this.base64toFile(fileField))
const photoRes = await uploadFile(form)
}
网友评论