【发布时间】:2019-04-26 18:46:55
【问题描述】:
我是 Angular 的新手,我想用头像保存一个新用户,那么如何将头像的 Blob 值传递给我的用户模型以便正确保存?
这是我的代码:
HTML
<input type="file" (change)="onSelectFile($event)" accept="image/x-png,image/gif,image/jpeg" value="{{image}}" id="avatar" ngModel name="avatar" #avatar="ngModel"/>
打字稿
image:any;
onSelectFile(event) {
var reader ;
reader = new FileReader;
reader.onload = (event) => {
console.log(reader.result);
this.image = reader.result;
console.log(this.image);
}
reader.readAsDataURL(event.target.files[0]);
}
保存用户时遇到的错误:
{"timestamp":"2018-11-24T13:29:13.222+0000","status":400,"error":"Bad Request","message":"JSON parse error: Cannot deserialize value of type `byte[]` from String \"\\fakepath\\IMG_20180808_023905.jpg\": Failed to decode VALUE_STRING as base64 (MIME-NO-LINEFEEDS): Illegal character ':' (code 0x3a) in base64 content; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `byte[]` from String \"\\fakepath\\IMG_20180808_023905.jpg\": Failed to decode VALUE_STRING as base64 (MIME-NO-LINEFEEDS): Illegal character ':' (code 0x3a) in base64 content\n at [Source: (PushbackInputStream); line: 1, column: 134] (through reference chain: org.vi.entities.User[\"avatar\"])","path":"/users"}
PS:user表中头像字段的类型是longblob
【问题讨论】:
标签: html angular typescript