HTML

<div class="row">
    <div class="mb-6">
        <label class="form-label" for="defaultFile">Upload file</label>
        <input type="file" class="form-control" id="defaultFile" (change)="onChange($event)">
        <hr>
        <button type="button" class="btn btn-outline-theme" (click)="uploadFile()">Upload</button>
   </div>
</div>

Typescript

import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-file-upload',
  templateUrl: './file-upload.component.html',
  styleUrl: './file-upload.component.scss'
})
export class FileUploadComponent {
    fileToUpload: File | null = null;

    constructor(private http: HttpClient) { }

    onChange(event:any) {
        this.fileToUpload = event.target.files[0];
    }

    uploadFile() {
        if (!this.fileToUpload) {
            alert('No file selected.');
            return;
        }

        const formData: FormData = new FormData();
        formData.append('file', this.fileToUpload, this.fileToUpload.name);

        return this.http.post("<ENDPOINT TO UPLOAD THE FILE>", formData).subscribe(
            (response) => {
                console.log(response);
            },
            (error) => {
                console.error(error);
            }
        );
    }
}

文件上传工作正常.不知道什么是"Angular "的方式来清除文本框.(为了保持这篇文章的简单性,我已经将调用API的代码移动到组件,并删除了所有错误处理等,以捕捉要点.

推荐答案

为了简单起见,我们可以在html中使用template reference变量#input,将元素引用传递到函数中并清除值,使用input.value = null

为了保持简单,我删除了API调用,并将其替换为of(true) to mock和API调用!

import { HttpClient } from '@angular/common/http';
import { Component } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { of } from 'rxjs';
import 'zone.js';

@Component({
  selector: 'app-root',
  standalone: true,
  template: `
    <div class="row">
        <div class="mb-6">
            <label class="form-label" for="defaultFile">Upload file</label>
            <input type="file" class="form-control" id="defaultFile" (change)="onChange($event)" #input>
            <hr>
            <button type="button" class="btn btn-outline-theme" (click)="uploadFile(input)">Upload</button>
      </div>
    </div>
  `,
})
export class App {
  fileToUpload: File | null = null;

  constructor() {}

  onChange(event: any) {
    this.fileToUpload = event.target.files[0];
  }

  uploadFile(input: any) {
    if (!this.fileToUpload) {
      alert('No file selected.');
      return;
    }

    const formData: FormData = new FormData();
    formData.append('file', this.fileToUpload, this.fileToUpload.name);

    return of(true).subscribe(
      (response: any) => {
        console.log(response);
        input.value = null; // <- changed here!
      },
      (error: any) => {
        console.error(error);
      }
    );
  }
}

bootstrapApplication(App);

Stackblitz Demo

Typescript相关问答推荐

如何根据参数的值缩小函数内的签名范围?

angular 17独立使用多个组件(例如两个组件)

类型安全JSON解析

React typescribe Jest调用函数

如何在使用`Next—intl`和`next/link`时导航

为什么tsx不判断名称中有"—"的属性?'

仅针对某些状态代码重试HTTP请求

如何在编译时为不带常量的参数引发错误

在函数中打字推断记录的关键字

如何避免推断空数组

寻址对象中路径的泛型类型

如何以Angular 导入其他组件S配置文件

从子类集合实例化类,同时保持对Typescript中静态成员的访问

为什么Typescript只在调用方提供显式泛型类型参数时推断此函数的返回类型?

在组件卸载时try 使用useEffect清除状态时发生冲突.react +打字

使用强制转换编写打字函数的惯用方法

如何在TypeScript类成员函数中使用筛选后的keyof this?

如何提取具有索引签名的类型中定义的键

为什么 Typescript 无法正确推断数组元素的类型?

如何在Typescript 中定义具有相同类型的两个泛型类型的两个字段?