【发布时间】:2019-01-01 14:55:50
【问题描述】:
我在表单中有一个文件上传。我还需要使用这个上传的文件和其他一些表单字段创建发布请求到后端。
下面是我的代码:
fileChange(event) {
const fileList: FileList = event.target.files;
if (fileList.length > 0) {
this.file = fileList[0];
this.form.get('file_upload').setValue(this.file);
}
}
onClick(){
const val = this.form.value;
const testData = {
'file_upload': this.file,
'id': val.id,
'name' : val.name,
'code': val.code,
};
this.http.post('https://url', testData,
.subscribe(
response => {
console.log(response);
});
}
除了上传的文件外,每个字段值都被发送到后端。我如何将上传的文件与表单字段一起发送到后端? 如果需要任何其他信息,请告诉我。
【问题讨论】:
标签: angular file-upload angular4-forms