gpt4 book ai didi

Angular7输入类型文件上传不起作用

转载 作者:行者123 更新时间:2023-12-02 16:06:52 25 4
gpt4 key购买 nike

现在我正在工作 Angular Angular 7 输入类型="file"不工作。

Angular6 工作正常。

Angular 6提交输入文件类型数据

我将得到这样的字段列表

enter image description here

但是 Angular 7 只能得到这样的图像路径。

enter image description here

只有我将 Angular 6 更新为 Angular 7 才会出现此错误。我不知道是什么问题。

谢谢

最佳答案

我的 Angular 应用程序中有一个上传文档部分,这是工作示例:

example.component.html

<form [formGroup]="form">
<div class="form-group">
<label>Select file to upload.</label>
<input type="file" id="bannedList" (change)="onFileChange($event);" #fileInput>
</div>
</form>
<button type="button" (click)="onSubmit();" [disabled]="!fileInput.value" class="btn btn-success pull-right"><i class="fa fa-save fa-fw"></i> Upload File</button>

example.component.ts

import { Component, OnInit, OnDestroy, ElementRef, ViewChild } from '@angular/core';
import { FormBuilder, FormGroup } from "@angular/forms";

...

export class exampleUploadComponent implements OnInit, OnDestroy {
public form: FormGroup;
@ViewChild('fileInput', { read: ElementRef }) private fileInput: ElementRef;

...

createForm() {
this.form = this.fb.group({
bannedList: null
});
}

onFileChange(event) {
this.uploadStatus = 0;
if (event.target.files.length > 0) {
let file = event.target.files[0];
this.form.get('bannedList').setValue(file);
}
}

private prepareSave(): any {
let input = new FormData();
input.append('bannedChequeCustomersFile', this.form.get('bannedList').value);
return input;
}

onSubmit() {
const formModel = this.prepareSave();
this.uploadChequeSubscription = this.chequeOperationService.uploadBannedChequeList(formModel).subscribe(
(res) => {
console.log("res handler:", res);
},
(err) => {
console.log("err handler:", err);
}
);
}

希望对你有帮助,谢谢。

关于Angular7输入类型文件上传不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53493693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com