html代码:
在:before-upload= " " 时进行操作,
<Upload
ref="upload"
:before-upload="handleBeforeUpload"
:show-upload-list="false"
:on-success="uploadSuccess"
:on-error="uploadError"
:format="['crt', 'cer']"
:max-size="10240"
:on-format-error="handleFormatError1"
:on-exceeded-size="handleMaxSize"
:headers="{ Authorization: token }"
v-model="formAdd.rootCertContent"
multiple
type="drag"
:action="uploadUrl"
style="display: inline-block; width: 58px"
>
vue代码:
// 转化 base64
handleBeforeUpload(file) {
// 清除上一次选择的文件 避免后面不在触发此事件
this.$refs["upload"].clearFiles();
this.rootFileName = file.name; // 上传文件名称
const reader = new FileReader();
//将文件读取为 DataURL 以data:开头的字符串
reader.readAsDataURL(file);
reader.onload = (e) => {
// 读取到的图片base64 数据编码 将此编码字符串传给后台即可
const code = e.target.result;
this.imgBase64 = code;
};
},