【问题标题】:How to upload image using angular 8如何使用 Angular 8 上传图片
【发布时间】:2020-04-15 17:45:55
【问题描述】:

app.module.ts

 ngOnInit() {
this.userForm = this.fb.group({
  firstName : ['', Validators.required],
  gender : ['', Validators.required],
  maritalStatus : ['', Validators.required],
  lastName : ['', Validators.required],
  dob : ['', Validators.required],
  nationality : ['', Validators.required],
  pic : [''],
  streetAddress : ['', Validators.required],
  city : ['', Validators.required],
  postalCode : ['', Validators.required],
  phone : ['', Validators.required],
  state : ['', Validators.required],
  country : ['', Validators.required],
  email : ['', Validators.required],
  jobTitle : ['', Validators.required],
  dateOfJoining : ['', Validators.required],
  department : ['', Validators.required],
  employeeStatus : ['', Validators.required],
  kra : ['', Validators.required],
  assignedSupervisor : ['', Validators.required],
  assignedSubordinate : ['', Validators.required],
  workExperience : ['', Validators.required],
  skills : ['', Validators.required],
  education : ['', Validators.required],
  password : ['', Validators.required]
})
}

onSubmit() {
this.submitted = true;
this.userService.addUser(this.userForm.value).subscribe(
  res => {
    this.model = res
    console.log(res)
  },
  error => {
    this.error = error,
    console.log(error)
  }
)

app.component.html

<div class="col-lg-12 col-12 mt-2">
 <label for="pic">Profile Picture</label><br/>
 <input type="file" formControlname='pic' name='pic'/>
</div>

app.service.ts

addUser(formData: User) {
return this.http.post<User>(`${this.serverUrl}`, formData).pipe(
  catchError(this.handleError)
);

}

一切正常。我只想知道如何以角度 8 上传图片。图片正在使用在邮递员上测试的节点上传到后端。任何人都可以帮助我如何使用角度上传图片,请帮助我

【问题讨论】:

    标签: angular typescript angular8 ng-file-upload


    【解决方案1】:

    在发出帖子请求之前,请将您的 formData 转换为实际的 FormData,如下所述:https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData

    使用

    addUser(formData: User) {
      const body = new FormData();
      for(const [key, value] of Object.entries(formData)){
         if(key === 'pic'){
            body.append(key, value)
         }
         body.append(key, `${value}`)
      }
      return this.http.post<User>(`${this.serverUrl}`, body).pipe(catchError(this.handleError);
    );
    

    那么您的后端应该能够识别该文件。 不幸的是,FormData 需要 stringblob 作为值的数据类型。

    虽然是这样说的:

    如果发送的值与 String 或 Blob 不同,则会自动转换为 String: (...)

    我的编辑器(与代码)告诉我不要使用数字,而是先转换它。

    解决方案是假设User 中的其他属性的数据类型是原语。

    【讨论】:

    • 它在 src/app/service/user.service.ts(26,25) 中给出错误错误:错误 TS2345:“用户”类型的参数不可分配给“字符串 |”类型的参数斑点'。 “用户”类型缺少“Blob”类型的以下属性:大小、类型、切片主体、.append('user', formData ==> Here Error)
    • 好的,所以你想上传一个用户对象,它有一个文件属性?
    • 是的,我想在 userForm 中上传具有上述所有字段的用户对象。休息一切正常
    • 我将它附加到 app.componenet.ts onSubmit() {const body = new FormData(); body.append(key,value)
    猜你喜欢
    • 2015-05-18
    • 2020-07-30
    • 2020-02-17
    • 2017-02-10
    • 2018-01-14
    • 1970-01-01
    • 2023-03-27
    • 2019-07-01
    • 1970-01-01
    相关资源
    最近更新 更多