Angular 5 文件上传:无法在“HTMLInputElement”上设置“值”属性

新手上路,请多包涵

我有一个用于在 Angular 5 应用程序中上传文件的表单,并且由于我已经完全从我之前编写的代码中复制了它,我可以发誓它以前曾工作过。

这是我的 HTML 代码:

 <form [formGroup]="form" (ngSubmit)="onSubmit()">
        <div class="form-group">
            <label>File:</label>
            <input #theFile type="file" (change)="onFileChange($event)" accept=".png" class="form-control"
                    formControlName="content" />
            <input type="hidden" name="fileHidden" formControlName="imageInput"/>

                    <!-- [(ngModel)]="model.content" -->

            <div class="alert alert-danger" *ngIf="!form.prestine && form.controls.content.errors?.noFile">
                Please provide a photo.
            </div>
            <div class="alert alert-danger" *ngIf="form.controls.content.errors?.fileTooBig">
                The file is too big and won't uploaded. Maximum allowed size is 500kb.
            </div>
        </div>
        <div class="form-group">
            <label>Notes</label>
            <textarea type="text" class="form-control" formControlName="notes" [(ngModel)]="model.notes" > </textarea>
        </div>
        <button type="submit" class="btn btn-primary" [disabled]="!form.valid">Submit</button>
        <button class="btn btn-default" type="button" (click)="close(false);">Cancel</button>
    </form>

这是 fileUpload 控件中使用的“onFileChange”方法:

 onFileChange($event)
  {
    if ($event.target.files.length > 0)
    {
        let ftu: File = null;
        ftu = $event.target.files[0];
        this.form.controls['content'].setValue(ftu);
        this.model.content = $event.target.files[0];
    }
  }

这是我编写和使用的自定义验证器的代码:

 import { FormControl } from '@angular/forms';

export class SekaniRootImageValidators
{
    static sizeTooBig(control: FormControl)
    {
        if (!control.value)
        {
            return { noFile : true }
        }
        else  if (control.value[0].size > 505000)
        {
            return { fileTooBig: true}
        }
        return null;

    }
}

现在的问题是,只要我在输入控件中选择一个文件,我就会在控制台中收到以下错误消息:

错误 DOMException:无法在“HTMLInputElement”上设置“value”属性:此输入元素接受文件名,该文件名只能以编程方式设置为空字符串。

这段代码以前工作过,所以我什至不知道从哪里开始。任何帮助表示赞赏!

注意: 这是一个有效答案的链接: Angular2:更改要上传的文件时不会触发 的验证

原文由 Behrooz 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 832
2 个回答

就像错误所说的那样,您只能将空字符串设置为文件输入值以清除选择。否则可能会带来安全风险。我无法想象该代码是如何工作的。也许在一些非标准(坏)浏览器中?

如果您只是删除该行,那么该代码是否应该工作,为什么您需要为它已经拥有的输入设置相同的值?

编辑:似乎需要自定义 ValueAccessor 来验证文件输入。另一个答案中的解决方案: Angular2:更改要上传的文件时不会触发 的验证

原文由 funkizer 发布,翻译遵循 CC BY-SA 3.0 许可协议

就我而言,我只是删除了 formControlName:

 <input type="file" (change)="onFileChange($event)">

和.ts:

 onFileChange(event) {
    const reader = new FileReader();

    if (event.target.files && event.target.files.length) {
      const [file] = event.target.files;
      reader.readAsDataURL(file);
      reader.onload = () => {
        this.data.parentForm.patchValue({
          tso: reader.result
        });

        // need to run CD since file load runs outside of zone
        this.cd.markForCheck();
      };
    }
  }

原文由 NicuVlad 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题