因为我正在使用angular5作为带有弹簧靴的前端和后端... 当iam通过表单数据上传文件时,.formData变空了。.
这是示例代码
onFileChanged(event: any) {
this.files = event.target.files;
console.log(this.files)
}
onUpload() {
const formData = new FormData;
for (let file of this.files) {
console.log('gdsgsfsd')
formData.append('hihi', file.name);
}
console.log('gdsgsfsd',formData)
}
<input id="file-input" type="file" (change)="onFileChanged($event)" multiple >
答案 0 :(得分:0)
将文件对象添加为append方法中的第二个参数。
onUpload() {
const formData = new FormData;
for (let file of this.files) {
console.log('gdsgsfsd')
formData.append('hihi', file, file.name);
}
console.log('gdsgsfsd', formData);
}