使用Angular Material 5上传文件

5
我尝试使用 Angular Material 5 上传文件(Angular 5)。
app.component.html
    <mat-horizontal-stepper [linear]="isLinear" #stepper="matHorizontalStepper">

  <mat-step [stepControl]="firstFormGroup">
    <form [formGroup]="firstFormGroup">
      <ng-template matStepLabel>Upload your audio file</ng-template>
      <mat-form-field>
          <input matInput  
          style="display: none" 
          type="file" (change)="onFileSelected($event)" 
          #fileInput name ="file" formControlName="firstCtrl" required>
      <button mat-button (click)="fileInput.click()" >Select File</button>
  </mat-form-field>
  <div>
    <button mat-button matStepperNext>Next</button>
  </div>
</form>

app.component.ts

export class AppComponent {
  selectedFile: File=null;
  isLinear = true;
  firstFormGroup: FormGroup;
  secondFormGroup: FormGroup;
  constructor(private _formBuilder: FormBuilder, private http: HttpClient) {}
  ngOnInit() {
   this.firstFormGroup = this._formBuilder.group({
     firstCtrl: ['', Validators.required]
    });
   this.secondFormGroup = this._formBuilder.group({
      secondCtrl: ['', Validators.required]
    });
  }

但是我遇到了这个错误。
ERROR Error: Input type "file" isn't supported by matInput.

了解,这段代码在没有使用Angular Material的情况下能够正常工作。有任何问题吗?

看起来不支持:https://material.angular.io/components/input/overview#supported-code-lt-input-gt-code-types - Fran
当然,Angular Material不支持这个。但是我正在寻找解决方案。 - K.cyrine
2个回答

5

我有同样的问题,

尝试这样做:

    <button mat-raised-button (click)="openInput()">Select File to Upload</button>
    <input id="fileInput" hidden type="file" (change)="fileChange($event.target.files)" name="file" accept=".csv,.xlsv">

  openInput(){ 
        document.getElementById("fileInput").click();
   }

上面的代码创建了一个材料按钮,并调用openInput()方法,该方法将该按钮替换为下面的HTML代码: 这对我很有用。
愉快的编码 ☻

在我的端上,这会导致以下错误:ERROR Error: mat-form-field必须包含一个MatFormFieldControl。 - Thomas
在应用程序模块中导入以下行,然后在ng模块中导入它们将解决您的问题。import { MatFormFieldModule, MatInputModule } from '@angular/material'; - Monkey D. Luffy

-1

Faster solution would be to use https://github.com/danialfarid/ng-file-upload :

<md-button class='md-raised md-primary' id='uploadFile' ngf-multiple='true' ngf-select='upload($files, $file, $event)'

type='file'> Upload File

else you would have to go to a custom code like this:

<label class="md-secondary md-raised md-button" md-ink-ripple for="input-file">
      <span>Select File to upload</span>
</label>
<input type="file" ngf-select ng-model="input-file" name="input-file" id="input-file">

编辑:

在你的HTML中:

 <input #file type="file" nbButton multiple (change)="upload(file.files)" /> 

然后在你的组件中:

upload(files: any) {
    this.yourServiceToUploadFiles.uploadFile(files).subscribe(
      (response: any) => { .......})}

然后在你的服务中:

uploadFile(files: any[]): Observable<HttpResponse<Blob>> {
    if (files.length === 0) {
      return;
    }

    const formData = new FormData();
    for (const file of files) {
      formData.append(file.name, file);
    }

    return this.http.post(`${apiUrl}/yourServiceEndPoint`, formData, {
      observe: "response",
      responseType: "blob"
    });
  }

这是一个AngularJS指令,我认为他正在使用Angular。 - ValLeNain
是的,它看起来相当老旧,让我更新我的答案。 - Tena

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接