如何以多部分格式Angular 5将文件(pdf,csv)发送到后端

时间:2018-09-06 11:19:36

标签: angular5 angular6

我想将文件发送到后端api并转换为多角5。这是我的代码: component.html:这是我的表单

<form [formGroup]="documentForm"  (ngSubmit)="uploaddocumentRequest()">
  <div class="text-center">
    <label for="filestyle-0">
        <span><i class="fa fa-upload"></i></span>
    </label>
    <input type="file" (change)="fileChange($event)"  name="file" id="boatimage" formControlName="document"/>   
  </div>
  <label>ADD TITLE</label>
  <input type="text" name="" class="form-control" placeholder="Type Here" formControlName="title"><br>
  <input type="submit" name="" value="UPLOAD DOCUMENT" class="form-control form-doc">
</form>

这是我的 component.ts

this.documentForm = this.formBuilder.group({
  title: '',
  document:''
});


fileChange(event){
   this.file = event.target.files[0]
}

uploaddocumentRequest(){
  var response =    this.doc.uploadLeadDocument(this.leadDetail.id,this.documentForm.value.title,this.file).then(data=>{
    if(data.code==200){
      console.log(data.document)            
    }else{}

  }) 
}

这是我的服务方法:

public uploadLeadDocument(lead_id,title,document){
    console.log(document)
    var hOptions = new Headers({
      'Accept': "application/json",
      'Access-Control-Allow-Origin': '*'
      });
   var reqBody = {lead_id:lead_id,title:title,file:document}
   return this.makeRequest("post",uploadDocumentApiUrl,reqBody,hOptions)
}

0 个答案:

没有答案