ElementUI中el-upload上传文件转base64格式

<el-upload class="upload-demo" ref="upload" action="" :on-change="httpRequest" :on-remove="httpRequest" :file-list="fileList" :auto-upload="false">
	<el-button slot="trigger" size="small" type="primary">选取文件</el-button>
</el-upload>
    data() {
		return {
			fileList: [],
			upLoadFile: []
		}
	},
	methods: {
		async httpRequest(file, fileList) {
			this.upLoadFile = []
			for (let i in fileList) {
				this.upLoadFile[i] = await this.getBase64(fileList[i].raw)
			}
			console.log('上传文件列表', this.upLoadFile)
		},
		// 转base64码
		getBase64(file) {
			return new Promise((resolve, reject) => {
				const reader = new FileReader()
				let fileResult = ''
				reader.readAsDataURL(file)
				// 开始转
				reader.onload = () => {
					fileResult = reader.result
				}
				// 转 失败
				reader.onerror = error => {
					reject(error)
				}
				// 转 结束
				reader.onloadend = () => {
					resolve(fileResult)
				}
			})
		},

猜你喜欢

转载自blog.csdn.net/qq_45020145/article/details/129242240