代码之家  ›  专栏  ›  技术社区  ›  Zabs

服务没有在4.x的角度上触发post请求

  •  0
  • Zabs  · 技术社区  · 6 年前

    uploadCSV 在应该发布到端点以上载csv的服务中-我可以在控制台日志中看到csvfile存在,但是当检查network选项卡时,没有显示任何post请求-有什么想法为什么?

    import { Injectable } from '@angular/core';
    import { ApiService } from "../../../services/api.service";
    import { Observable } from "rxjs/Observable";
    import { HttpClient } from '@angular/common/http';
    
    
    @Injectable()
    export class BulkuploadService {
    
    constructor(
        private http: HttpClient,
        private apiService: ApiService,
        private userService: UserService
    ) { }
    
    uploadCSV(csvFile: any): any {
        console.log(csvFile);
        let formData:FormData = new FormData;
        formData.append('file', csvFile, csvFile.name);
        console.log(formData);
    
        let url = this.apiService.getApiUrl('bulk-upload/upload');
        console.log(url);
    
        return this.http.post(url, formData);
    }
    
    }
    

    //组成部分

    import { Component, 
    OnInit, 
    Inject,
    Input, 
    Output, 
    EventEmitter, 
    ElementRef } from '@angular/core';
    
    import { DOCUMENT } from '@angular/common';
    import * as moment from 'moment';
    import { BulkuploadService } from './bulkupload.service';
    
    @Component({
    selector: 'app-bulkupload',
    templateUrl: './bulkupload.component.html',
    styleUrls: ['./bulkupload.component.scss'],
    providers: [
        BulkuploadService
    ]
    })
    export class BulkuploadComponent implements OnInit {
    
    @Output() closeEvent = new EventEmitter<boolean>();
    private postObjects = [];
    
    constructor(@Inject(DOCUMENT) private document: any,
        private ref: ElementRef,
        public bulkService: BulkuploadService    
    ) { }
    
    uploadCSV(event: any): void {
        this.bulkService.uploadCSV(event.target.files[0]);
    }
    
    }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Cam Plimsoll    6 年前

    可观测数据是懒惰的,你需要订阅它才能执行。

     uploadCSV(event: any): void {
        this.bulkService.uploadCSV(event.target.files[0]).subscribe(data => {
          //handle next steps after execution
        },err=>{
          //handle error
        },()=>{
          //completed
        });
      }
    
        2
  •  1
  •   Ali Shahzad    6 年前

    您需要订阅http post调用,如 this

    showConfig() {
      this.configService.getConfig()
        .subscribe((data: Config) => this.config = {
            heroesUrl: data['heroesUrl'],
            textfile:  data['textfile']
        });
    }