Я хочу отправить файл в 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)
}