字符串和文件base64转换

字符串
字符串转化为base64: window.btoa(str)
base64转化为字符串: window.atob(str)

文件

const reader = new FileReader()
reader.readAsDataURL(file)
this.productData.fileNote = null
// 注意此方法是异步的 若要卸载外面需要将其改写为promise
reader.onload = (e) => {
    
    
	this.base64fileurl = e.target.result // 将base64编码赋值给对象
	// 如果想截取base64字符串头
	this.base64fileurl = this.base64fileurl .substring(this.base64fileurl.indexOf(',') + 1)
}

猜你喜欢

转载自blog.csdn.net/weixin_52443895/article/details/130336985