Javascript 角度5视图子参考不适用于Safari(v 11.1)
我有一个简单的文件输入,我想使用Javascript 角度5视图子参考不适用于Safari(v 11.1),javascript,angular,safari,angular5,Javascript,Angular,Safari,Angular5,我有一个简单的文件输入,我想使用ElementRef和@ViewChild访问它,并将图像上传到服务器 我得到了以下解决方案,它在Chrome和Firefox的最新版本中运行良好,但在Safari(v11.1)中不起作用 问题是在Safari中,输入的ElementRef不包含任何文件。 当我将this.el.nativeElement.files.length记录到更改操作中时,其结果在Safari中为-1,在Chrome或Firefox中为1。你知道这个问题吗 <input #file
ElementRef
和@ViewChild
访问它,并将图像上传到服务器
我得到了以下解决方案,它在Chrome和Firefox的最新版本中运行良好,但在Safari(v11.1)中不起作用
问题是在Safari中,输入的ElementRef不包含任何文件。
当我将this.el.nativeElement.files.length
记录到更改操作中时,其结果在Safari中为-1,在Chrome或Firefox中为1。你知道这个问题吗
<input #fileInput (change)="fileUpload()" type="file" name="pic" accept="image/*">
我正在使用Angular client 1.7.4和Angular 5.2.0
谢谢 如果要以angular方式上载文件,则必须使用$event对象来获取文件 HTML
<input (change)="fileUpload($event.target.files) type="file" name="pic" accept="image/*">
例如:
<input (change)="fileUpload($event.target.files) type="file" name="pic" accept="image/*">
fileUpload(files: File[]){
console.log(files)
var formData = new FormData();
Array.from(files).forEach(f => formData.append('file',f))
this.http.post('https://file.io', formData, {reportProgress: true, observe: 'events'})
.subscribe(event => {
if (event.type === HttpEventType.UploadProgress) {
this.percentDone = Math.round(100 * event.loaded / event.total);
} else if (event instanceof HttpResponse) {
this.uploadSuccess = true;
}
});
}