如何使用 `POST` 请求将 3 个参数传递给后端?
How to pass 3 argument to backend with `POST` request?
根据后端,我要求通过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 个参数。正确的做法是什么?
有人帮帮我吗?
这是我的工作示例
this.http.post<Customer>(this.base_url + 'v1/customers', client, this.getHeaders());
其中 client 是客户对象,this.getHeaders() 是:
getHeaders() {
return {
headers: new HttpHeaders({
'Content-Type': 'application/json; charset=utf-8',
})
};
}
祝你好运!
我建议将所有内容包装在一个对象中。并将其发送到后端。
或者只发送上传数据
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);
根据后端,我要求通过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 个参数。正确的做法是什么?
有人帮帮我吗?
这是我的工作示例
this.http.post<Customer>(this.base_url + 'v1/customers', client, this.getHeaders());
其中 client 是客户对象,this.getHeaders() 是:
getHeaders() {
return {
headers: new HttpHeaders({
'Content-Type': 'application/json; charset=utf-8',
})
};
}
祝你好运!
我建议将所有内容包装在一个对象中。并将其发送到后端。
或者只发送上传数据
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);