如何通过`POST`请求 将3参数传递给后端?

问题描述 投票:0回答:2

根据后端,我要求通过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个参数。这样做的正确方法是什么?

任何人帮助我?

angular7 angular-httpclient
2个回答
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
投票

这是我的工作范例

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',
      })
    };
  }

祝好运!

© www.soinside.com 2019 - 2024. All rights reserved.