角度文件上传到API

时间:2018-10-22 05:09:39

标签: angular api file-upload

我是Angular的初学者,我想知道如何创建Angular-6文件上传部分。我需要将上传的角度文件发送到API。 我尝试了下面的代码,但对我来说不起作用。 我正在使用角度形式。 I've tried this code from here.

upload.component.html

   struct MyWeather : Decodable {
    let coord : Coordinate?
    let weather : [Weather]?
    let base : String?
    let main : Main?
    let visibility: Int?
    let wind : Wind?
    let clouds : Clouds?
    let dt : Date?
    let sys : Sys?
    let id : Int?
    let name : String?
    let cod : Int?
}

struct Coordinate : Decodable {
    let lat : Double?
    let lon : Double?
}

struct Weather : Decodable {
    var id : Int?
    var main, myDescription, icon : String?

    enum CodingKeys : String, CodingKey {
        case id = "id"
        case main = "main"
        case icon = "icon"
        case myDescription = "description"
    }

}

struct Main : Decodable {
    let temp : Double?
    let pressure : Int?
    let humidity : Int?
    let temp_min : Double?
    let temp_max : Double?
}

struct Wind : Decodable {
    let speed : Double?
    let deg : Int?
}

struct Clouds: Decodable {
    let all : Int?
}

struct Sys : Decodable {
    let type : Int?
    let id : Int?
    let message : Double?
    let country : String?
    let sunrise : Date?
    let sunset : Date?
}

upload.component.ts

this.myForm = this.builder.group({
   country:['',Validators.required],
   age:['',Validators.required],
   dependentName:['',Validators.compose([Validators.maxLength(20), Validators.required])]
})

upload.service.ts

<mat-card>
 <mat-card-title>Upload Files</mat-card-title>
 <mat-card-subtitle></mat-card-subtitle>
   <form [formGroup]="form" (ngSubmit)="onSubmit()">
   <mat-card-content>
     <div fxLayout="row" fxLayoutWrap="wrap">
       <div fxFlex.gt-sm="80%" fxFlex.gt-xs="50" fxFlex="100">
         <div class="mb-1">
           <input type="file"  id="file" formControlName="file 
             (change)="handleFileInput($event.target.files)">
         </div>
        </div>
      </div>
  </mat-card-content>
  <mat-card-actions class="px-1">
    <button mat-raised-button color="primary" type="submit 
    [disabled]="!form.valid">Submit</button>
  </mat-card-actions>
 </form>
 <div class="container">
  <router-outlet></router-outlet>
 </div>
</mat-card>

有人可以帮助我吗? 非常感谢

0 个答案:

没有答案