【问题标题】:Set the value of file input in Angular 6在 Angular 6 中设置文件输入的值
【发布时间】:2018-09-17 15:21:42
【问题描述】:

我正在使用 Angular 6,我想获取放在 div 中的图像并将其设置为表单的 input type="file" 中的值。

因此,用户将图像放在div 中,文件输入将图像作为值获取,然后我可以提交表单。

这是代码

<form [formGroup]="imageUpload" (ngSubmit)="imageUploadSubmitted()">
  <div id="imageDrop" (drop)="drop($event)" (dragover)="allowDrop($event)" #imageDrop></div> 

  <input type="file" formControlName="imageInput" required #imageInput id="imageInput"  >
  <label>
    <input type="text" formControlName="imageName" placeholder="name" required >
  </label>                      

  <button type="submit" [disabled]="!imageUpload.valid">Submit</button>
</form>

组件是

  allowDrop(e) {
    e.preventDefault();
  }

  drop(e) {
    e.preventDefault();  
    this.imageUpload.controls.imageInput.reset();  
    this.imageDrop.innerHTML="";    
    let input = this.imageUpload.controls.imageInput as any;
    input.value = e.dataTransfer.files[0];  
    this.readfiles(e.dataTransfer.files);
  }

  readfiles(files){
    const reader = new FileReader();
    let image = new Image();
    reader.onload =  (event) =>{
      this.imageDrop.nativeElement.innerHTML="";                
      let fileReader = event.target as FileReader;
      image.src = fileReader.result;
      image.width = 150; 
      this.imageDrop.nativeElement.appendChild(image);  
      /*if (this.imageUpload.controls.imageInput.value==null) {                
        let input = this.imageUpload.controls.imageInput as any;        
        input.value = event.target.result;  
      }  */
    };
    reader.readAsDataURL(files[0]);    

  }

drop 中的input.value 总是有一个值,但我猜是wrog 类型的值。表单的提交按钮未激活。

那么也许我需要读取图像以将其设置为一个值,所以我将转移readfiles 中的逻辑 - 检查注释掉的代码。这不起作用,因为我在 input.value = event.target.result;result 上收到错误。错误是Property result does not exist on type EventTarget

如何解决这个问题并将图像设置为文件输入值?

谢谢

PS,这里是stackblitz

【问题讨论】:

    标签: angular typescript input angular6


    【解决方案1】:

    你想在 imageInput formControl 中找到什么值?

    使其工作的一种方法是将 input.value 更改为

    input.setValue(e.dataTransfer.files[0]);
    

    这样,formControl 将正确更新,不再无效......但是由于您使用输入文件,它将无法正常工作(它会告诉您他想要一个文件名)

    但实际上您并没有真正将输入文件用作输入文件,因此您可以通过删除 type="file" 来更改 HTML 中的输入,并且您的表单应该可以工作;)


    在这里你可以找到我自己使用 ngModel 或 formControlName 的图像选择器,它可以使用 base 64 初始化: https://github.com/xrobert35/asi-ngtools/tree/angular6-compatibility/src/components/asi-image-chooser 和一个工作演示: https://ng-tools.asi.fr/views/showroom/asi-ngtools/components/asi-image-chooser

    【讨论】:

    • 您好。谢谢(你的)信息。问题是我尝试使用setValue 并得到错误。在 stackblitz 中,如果我尝试 setValue 我会得到 ERROR Error: The operation is insecure. 。 Chrome 说 Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programmatically set to the empty string. 。我没有测试删除type="file",因为我希望imageDrop 可以点击并且能够调用imageInput 文件选择器。
    • 我正在考虑一个解决方案,但除了使用隐藏的输入来存储数据或像我一样使用 ControlValueAccessor 创建一个组件以使其适用于 formControlName 并存储数据之外,我实际上什么也看不到你想要
    • 另外,我认为formidable 需要查看type="file" 才能识别表单中的文件。所以我根本无法删除type="file"。顺便说一句,这是我的 original question ,这个问题来自哪里。它可能会帮助您更好地了解我想要做什么并帮助您思考解决方案。现在,我将检查您的链接并让您知道。谢谢
    • 没关系,你刚才告诉我的任何解决方案,我现在迷路了,无法解决这个问题,所以任何想法都可以。
    猜你喜欢
    • 2016-06-27
    • 2016-11-16
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    • 1970-01-01
    • 2018-01-14
    • 2022-01-03
    相关资源
    最近更新 更多