Javascript 图像上传功能在angular 6中不起作用

Javascript 图像上传功能在angular 6中不起作用,javascript,angular,angular6,Javascript,Angular,Angular6,我使用“ngx图像裁剪器”(ngx image cropper)裁剪图像,并使用图像的base64值将其发送到服务器。问题是图像有时会给出空值。我找不到这种行为背后的原因。我正在使用angular的“Domsanizer”上传图像,并将其标记为angular上传的“安全”。 这是我的密码 HTML: ts: 从“ngx图像裁剪器”导入{ImageCroppedEvent}; 从“@angular/platform browser”导入{domsanizer}; 从“../../common/c

我使用“ngx图像裁剪器”(ngx image cropper)裁剪图像,并使用图像的base64值将其发送到服务器。问题是图像有时会给出空值。我找不到这种行为背后的原因。我正在使用angular的“Domsanizer”上传图像,并将其标记为angular上传的“安全”。 这是我的密码 HTML:


ts:
从“ngx图像裁剪器”导入{ImageCroppedEvent};
从“@angular/platform browser”导入{domsanizer};
从“../../common/constants/placeholder image”导入{defaultImage};
@组成部分({
选择器:“应用程序学生表单”,
templateUrl:'./student form.component.html',
样式URL:['./学生表单.component.scss']
})
导出类StudentFormComponent实现OnInit{
学生照片:弦;
图像预览:字符串| ArrayBuffer;
裁剪图像:字符串;
imageChangedEvent:事件;
公共图像路径:任何;
建造商(专用注射器:注射器、,
公共消毒剂(消毒剂){
this.imagePath=“../../../assets/imgs/placeholder.png”;
}
submitHandler(){
如果(!this.cropeImage){
log(“未找到图像默认图像”);
让safeimage=this.sanitizer.bypassSecurityTrustResourceUrl(defaultImage);
this.admissionForm.value.studentPhoto=safeimage['changingThisBreaksApplicationSecurity';
}
}
imageCropped(事件:ImageCroppedEvent){
this.cropeImage=event.base64;
this.admissionForm.controls.studentPhoto.updateValueAndValidity();
让safeimage=this.sanitizer.bypassSecurityTrustResourceUrl(this.cropeImage);
this.admissionForm.value.studentPhoto=safeimage['changingThisBreaksApplicationSecurity';
}
loadImageFailed(){
日志(“加载失败”);
}
fileChangeEvent(事件){
常量文件:file=event.target.files[0];
this.admissionForm.value.studentPhoto=文件;
this.imageChangedEvent=事件;
//log(“图像数据”,this.admissionForm.value.studentPhoto);
}
imageLoaded(){
//显示裁剪器
console.log(“图像加载”);
}
cropperReady(){
//收割机就绪
console.log(“已加载裁剪器”)
}
}

因为您已经在这里看到了图像。您可以使用FormData附加图像,然后提交到服务器

 submitHandler() {

        if (!this.croppedImage) {
          console.log("NO image found default image");
          let safeimage = this.sanitizer.bypassSecurityTrustResourceUrl(defaultImage);
          this.admissionForm.value.studentPhoto = safeimage['changingThisBreaksApplicationSecurity'];

          let fd = new FormData();
          fd.append("file", safeimage);
            // call http request to post image to server
        }
      }
 submitHandler() {

        if (!this.croppedImage) {
          console.log("NO image found default image");
          let safeimage = this.sanitizer.bypassSecurityTrustResourceUrl(defaultImage);
          this.admissionForm.value.studentPhoto = safeimage['changingThisBreaksApplicationSecurity'];

          let fd = new FormData();
          fd.append("file", safeimage);
            // call http request to post image to server
        }
      }