离子输入文件不清除对象



在我的ionic应用程序中,我有一个home.html文件:<input type="file" (change)="onFileChange($event)" multiple />

和在.ts文件中

onFileChange(fileChangeEvent) {
this.file = fileChangeEvent.target.files[0];
if (typeof fileChangeEvent.target.files[1] !== 'undefined') {
this.file_2 = fileChangeEvent.target.files[1];
}
if (typeof fileChangeEvent.target.files[2] !== 'undefined') {
this.file_3 = fileChangeEvent.target.files[2];
}}async submitForm() {
let formData = new FormData();
formData.append("photo", this.file, this.file.name);
if (typeof this.file_2 !== 'undefined') {

formData.append("photo_2", this.file_2, this.file_2.name);
}
if (typeof this.file_3 !== 'undefined') {

formData.append("photo_3", this.file_3, this.file_3.name);
}
formData.append("nome_mercatino", this.nome_mercatino);
this.http.post("https://myserver/myphpfile.php", formData).subscribe((response) => {
console.log(response);
});

}`

将1到3个图像发送到服务器。当我第一次发送文件时,没有问题,但是,如果我重试,例如,从2个文件更改为仅1个文件,当我将我的文件(也是上次发送的第二个文件(发送到服务器时,会重新发送。为什么?如何清除文件对象以始终发送正确的文件?

我得到的答案是,当你完成时,将所有三个文件都设置为未定义。你正在设置file_2,但在这种情况下,它失败了,而file_2没有改变,我想你明白了吗?

onFileChange(fileChangeEvent) {
this.file = fileChangeEvent.target.files[0];
if (typeof fileChangeEvent.target.files[1] !== 'undefined') {
this.file_2 = fileChangeEvent.target.files[1];
}
if (typeof fileChangeEvent.target.files[2] !== 'undefined') {
this.file_3 = fileChangeEvent.target.files[2];
}}async submitForm() {
let formData = new FormData();
formData.append("photo", this.file, this.file.name);
if (typeof this.file_2 !== 'undefined') {

formData.append("photo_2", this.file_2, this.file_2.name);
}
if (typeof this.file_3 !== 'undefined') {

formData.append("photo_3", this.file_3, this.file_3.name);
}
formData.append("nome_mercatino", this.nome_mercatino);
this.http.post("https://myserver/myphpfile.php", formData).subscribe((response) => {
console.log(response);
this.file_1=undefined;
this.file_2=undefined;
this.file_3=undefined;
});

最新更新