你好朋友,我创建了一个用角度4创建的项目,我想上传图像并将请求发送到PHP文件,但是表单数据无法附加值和图像。 请帮我 我包括FormGroup,FormControl,FormBuilder,验证器,Http
[1,2]
[3,4]
答案 0 :(得分:1)
您是否尝试仅传递图像的名称和值?
对于文件管理(通常),这对我来说很有效:
formData.append('uploadFile', ImageFile);
答案 1 :(得分:1)
HTML
<input #fileSelect type="file" class="form-control" (change)="onFileChanged($event)" accept=".jpg, .png"/>
组件
export class FileUploadComponent {
@ViewChild('fileSelect') fileSelectInput: ElementRef;
fileToUpload: any;
onFileChanged(event) {
// https://stackoverflow.com/questions/13602039/e-srcelement-is-undefined-in-firefox
this.fileToUpload = (event.srcElement || event.target).files;
let formData: FormData = new FormData();
formData.append('file', this.fileToUpload[0]);
this.createOrUpdateResource(formData);
}
// https://stackoverflow.com/questions/48059121/angular4-file-upload-put-request-fails,
so making this POST
private createOrUpdateResource(formData: FormData) {
this.http.post<any>(`http://localhost:8080/upload`, formData).subscribe((res) => {
//success
}, error => {
//error
});
}
}