首页 > 解决方案 > 关于 .NET core API 和 Angular 中的图片上传和获取文件信息模型

问题描述

在我的一项功能中,我需要添加图像以及其他表单数据,因此我使用表单数据来发送文件。在获取请求中,我只需要显示图像,以便获取图像信息,例如存储的路径、显示图像的名称。我创建了 3 个单独的模型,即 BaseModel、GetModel 和 PostModel,它们在获取和发布时只有 1 个参数差异。这是我的模型。

 public class PatientInformationBaseViewModel
    {
        public Guid id { get; set; }
        public string firstName { get; set; }
        public string lastName { get; set; }
        public string email { get; set; }
        public int age { get; set; }
        public string phone { get; set; }
        public GenderViewModel gender { get; set; }
        public string history { get; set; }
        public string caseNo { get; set; }
    }

public class GetPatientInfomationViewModel: PatientInformationBaseViewModel
    {
        public FileInformationViewModel patientPhotoInformation { get; set; }
    }

 public class PostPatientInformationViewModel : PatientInformationBaseViewModel
    {
        [JsonIgnore]
        public IFormFile patientPhoto { get; set; }
    }

我创建了一个模型来接受控制器中的表单数据

public class PatientInformationFormDataViewModel
    {
        public string patientInformation { get; set; }
        public IFormFile patientPhoto { get; set; }
    }

在控制器中,我将反序列化为 post 模型,然后将其发送到我的服务层。在我的服务层中,我将患者信息添加到数据库并返回 GUID,它是我的表的主键。使用 Id 字段我正在上传图像,即将文件保存到文件系统并返回详细信息,然后调用查询以将文件信息保存在表中。

在获取要显示在 Angular 表中的数据时,我正在使用 GetModel,因为我不需要实际文件。如果我结合所有模型,其中一个参数将无用并保持为空,所以我选择了这个方法。

这是正确的方法还是在约定方面有更好的方法。先感谢您

标签: angularasp.net-core.net-coreasp.net-core-webapi

解决方案


推荐阅读