如何将3个参数传递给带有POST请求的后端?

时间:2019-03-25 07:25:57

标签: angular7 angular-httpclient

根据后端,我需要通过post请求传递3个参数,这个后端函数是:

public ResponseModel Post([FromBody] CourseFileUpload item, string fileName, Stream fileToUpload) 

现在我正试图像这样传递参数:

uploadFile(uploadData:ModelToFileSteam):Observable<ModelToFileSteam> {
        const fileName = uploadData.fileName;
        console.log('file name is', fileName);
        const headers = new HttpHeaders({ 'Content-Type': 'application/json', 'Access-Control-Allow-Origin':'*' });
        return this.http.post<ModelToFileSteam>(environment.baseUrl+`CourseFileUpload`, uploadData.fileToUpload, uploadData.fileName, uploadData.uploadStream)
        .pipe(
            map(data => {
                return data;
            } ),
            catchError(this.handleError)
        )
    }

但是遇到错误,根本无法传递3个参数。正确的方法是什么?

有人帮我吗?

2 个答案:

答案 0 :(得分:3)

我建议将所有包装在单个对象中。并将其发送到后端。

或者只是发送uploadData

return this.http.post<ModelToFileSteam>(environment.baseUrl+`CourseFileUpload`, uploadData)
        .pipe(
            map(data => {
                return data;
            } ),
            catchError(this.handleError)
        )

在后端,您可以获得类似req.body.uploadData的uploadDate 要检查您可以console.log(uploadData.fileName);

答案 1 :(得分:1)

它是我的工作示例

this.http.post<Customer>(this.base_url + 'v1/customers', client, this.getHeaders());

其中客户端是客户对象,而this.getHeaders()是:

  getHeaders() {
    return {
      headers: new HttpHeaders({
        'Content-Type':  'application/json; charset=utf-8',
      })
    };
  }

祝你好运!