【发布时间】:2022-07-13 16:14:04
【问题描述】:
我想将图像详细信息存储在数据库中,但无法在控制器中获取从 angular 发送的图像数据 我可以使用控制器参数中的 [FromForm] 属性获取图像信息。但我希望它在模型中。提前致谢。
这是 Asp.net core 中的模型
public class FileModel
{
public FileModel()
{
SpouseDetails = new SpouseDetailsData();
ChildrensDetails = new List<ChildrensDetailsDataModel>();
}
public IFormFile? Photo { get; set; }
//public byte[]? PhotoData { get; set; }
public int UserDetailId { get; set; }
public List<ChildrensDetailsDataModel>? ChildrensDetails { get; set; }
public SpouseDetailsData SpouseDetails { get; set; }
public class ChildrensDetailsDataModel
{
public string ChildCountry { get; set; } = null!;
public string ChildCity { get; set; } = null!;
public string ChildState { get; set; } = null!;
public string ChildPhoneNumber { get; set; } = null!;
public DateTime ChildDOB { get; set; }
public string ChildLastName { get; set; }
public string ChildEmailAddress { get; set; } = null!;
public string ChildFirstName { get; set; } = null!;
}
public class SpouseDetailsData
{
public string SpouseEmail { get; set; } = null!;
public string? SpouseCity { get; set; }
public string? SpouseState { get; set; }
public string? SpouseCountry { get; set; }
public string SpouseHometown { get; set; } = null!;
public string SpouseFirstName { get; set; } = null!;
public string SpouseLastName { get; set; } = null!;
public DateTime SpouseDob { get; set; }
}
}
这是 Angular 代码
composeModel(): void {
this.updateUserModel.firstName = this.profileForm.value.firstName;
this.updateUserModel.lastName = this.profileForm.value.lastName;
this.updateUserModel.socialMedia = this.profileForm.value.firstName;
this.updateUserModel.state = this.profileForm.value.state;
this.updateUserModel.dob = this.profileForm.value.DOB;
this.updateUserModel.city = this.profileForm.value.city;
//Spouse Details
this.updateUserModel.spouseDetails = this.spouseDetails;
this.updateUserModel.photoData=this.selectedFile;
this.updateUserModel.childrensDetails = this.profileForm.value.childrensDetails;
}
角度模型
export class UserDetailModel {
childrensDetails: ChildrensDetailsModel[]=[];
spouseDetails!: SpouseDetails
id:string="";
photoData!:File;
photo!:FormData
}
这是控制器
[HttpPost]
[Route("UpdateUser")]
public async Task<JsonResult> UpdateUser([FromBody] FileModel userDetailModel)
{
var userslist = await _userBusiness.UpdateUser(userDetailModel);
return new JsonResult(new { userslist });
}
角度服务文件
updateUser(userDetailModel:any):Observable<any> {
return this.http.post<any>(`${UserProfileURLConstants.USER_PROFILE}`,userDetailModel);
}
【问题讨论】:
标签: angular typescript asp.net-core