Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度5视图子参考不适用于Safari(v 11.1)_Javascript_Angular_Safari_Angular5 - Fatal编程技术网

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;
        }
    });
  }