html - 使用角度上传带有base64的图像时,假路径存储在mongodb中
问题描述
Angular:
I'm trying to upload an image through angular using base64 format but in MongoDB, it stores the fake path i.e 'c://fakepath/filename.jpg' instead of storing base64 string
onSubmit(): any {
// this.url=this.newuserForm.value;
// this.newuserForm.value.file =this.url
// let d=sessionStorage.getItem('image');
// this.newuserForm.value.files=d;
// console.log("before fetch"+this.newuserForm.value.file);
this.crudService.AddUser(this.newuserForm.value)
.subscribe((d) => {
console.log('Data added successfully!');
console.log(d);
// this.url=d.files;
console.log("new file" + d.file);
this.ngZone.run(() => this.router.navigateByUrl('/useraction'))
}, (err) => {
console.log(err);
});
}
// file upload
onSelectFile(event: any) {
if (event.target.files && event.target.files[0]) {
var reader = new FileReader();
reader.readAsDataURL(event.target.files[0]); // read file as data url
reader.onload = (event: any) => { // called once readAsDataURL is completed
this.url = event.target.result;
//store image from base 64 format
console.log("my ts file url is " + this.url);
sessionStorage.setItem('image', this.url)
this.newuserForm.value.file = this.url;
}
}
}
Html :
这是使用我正在尝试上传文件的 Html 文件
label class="hoverable" for="fileInput">
<img name="image" [src]="url ? url : 'https://www.w3schools.com/howto/img_avatar.png'" width="50px" height="50px">
<div class="hover-text">Change Image
<!-- <img class="camimg" src="assets/cameraicon.jpg" /> -->
</div>
<div class="background"></div>
</label>
<br/>
<input id="fileInput" type='file' formControlName="file" (change)="onSelectFile($event)">
CrudeService.ts 这是一个 crud 服务文件,我正在尝试调用后端 API 和后端模型我已将文件类型作为字符串 // 添加用户 AddUser(data: Users): Observable { let API_URL = ${this.REST_API}/add-user
; 返回 this.httpClient.post(API_URL, data) .pipe( catchError(this.handleError) ) }
解决方案
推荐阅读
- javascript - 无法插值
- android-nestedscrollview - 在 NestedScrollView 中使用 PagedListAdapter
- ios - 如何设置应用程序委托中可访问的 XCUItest 类的参数
- html - 防止强制字段星号标签换行
- java - 在不使用任何文件路径的情况下解码和解压缩文件
- c# - Caliburn.Micro WPF:如何创建具有依赖项的新 ViewModel?
- python - 使用不区分大小写的字符串访问python字典
- mysql - 如何使用 Ansible 为 MySQL/MariaDB root 用户设置和保存随机密码?
- c# - 突出显示 textbox-winform c# 中的错误行
- angular - Angular 4+ Dependency Injection - Getting the closest host component out of more possibilities