首页 > 解决方案 > 如何修复离子输入类型文件中未定义的属性文件

问题描述

我正在开发 Ionic 3 移动应用程序,我想将图像从移动应用程序上传到 Web API。为此,我使用 POST 方法发送图像。我正在尝试将图像作为多部分文件上传,因为服务器需要一个多部分文件。因此我添加了一个输入字段来选择文件,例如。接下来,我创建了changeListener()将图像发送到服务器的事件。

<ion-input type="file" accept="image/*" id="upload" [(ngModel)]="imagePath" (ionChange)="changeListener($event)"></ion-input>


changeListener($event): void {
     this.imagePath = $event.target.files[0];
     console.log($event.target.files[0])
     this.imageProvider.uploadImage(this.imagePath)
}

当我选择文件时,它给了我一个 typeError,例如“无法读取未定义的属性文件”。任何人都可以帮助解决这个问题吗?

标签: angulartypescriptionic3

解决方案


更改ionChangechange

<ion-input type="file" accept="image/*" id="upload" 
        [(ngModel)]="imagePath" 
        (change)="changeListener($event)"></ion-input>

工作示例


推荐阅读