angular4上传图片并传递给服务器并保存到数据库中(express和mongoose,mongodb)

时间:2018-01-04 01:23:00

标签: mongodb angular express mongoose

有没有人知道如何在Angular 4环境中上传图像,然后将这些数据作为快速服务器发送到后端,并使用mongoose存储到mongodb中?如果有人知道,请发布答案。感谢

文件输入

{{imgfile.errors | json}}
<div class="alert alert-danger"
*ngIf="size >= 300000">
  <strong>
    Can't upload this image. check image size!!!
  </strong>
</div>

脚本

onFileChange(event) {
let reader = new FileReader();
if(event.target.files && event.target.files.length > 0) {
  console.log(event.target.files)
  let file = event.target.files[0];enter code here
  this.size = file.size;
  console.log(file.size);
  if (file.size <= 300000) {
    this.picSize = true;
  }
  reader.onload = () => {
    this.new_food.image = reader.result;
  };
  reader.readAsDataURL(file);
}

}

1 个答案:

答案 0 :(得分:0)

  onFileChange(event) {
    let reader = new FileReader();
    if(event.target.files && event.target.files.length > 0) {
      console.log(event.target.files)
      let file = event.target.files[0];enter code here
      this.size = file.size;
      console.log(file.size);
      if (file.size <= 300000) {
        this.picSize = true;
      }
      reader.onload = () => {
        this.new_food.image = reader.result;
      };
      reader.readAsDataURL(file);
    }

  }