【发布时间】:2019-09-19 04:27:15
【问题描述】:
我想将带有其他表单详细信息的图像文件作为一个对象发布如下
export interface Employee {
id: Guid;
firstName: string;
lastName: string;
email: string;
imgFile: File;
enteredDate: Date;
}
这是我的服务
addEmployee(employee: Employee) {
return this.http.post(this.baseUrl + 'employees/add', employee);
}
是否可以像上面那样发送文件?如果可能,如何在 asp.net core web api 中检索该文件?
[HttpPost("add")]
public void Add(EmployeeAddDto employeeAddDto)
{
....// save employeeAddDto object to database
}
EmployeeAddDtoimgFile的文件应该是什么?
【问题讨论】:
-
您可以通过使用
FormData来检查此post-formdata -
如果您的 web-api 仅接受 json,那么您将无法将图像文件作为表单数据或图像作为 json 发送为此您几乎没有选项,例如具有 json 或 formdata 类型的用户 web-api,或者您可以首先将图像转换为base64,然后通过json发送,一旦你得到base64字符串服务器端解码回base64的图像然后我会工作
标签: angular typescript asp.net-core-webapi