.net 我怎样才能上传照片从角,网?

r8xiu3jd  于 2023-05-02  发布在  .NET
关注(0)|答案(1)|浏览(161)

使用此代码,我无法从angular上传文件到。我从angular中的input获取文件,然后将表单发送到。net。
我有registerForm在Angular ,其中包含姓名,姓氏,照片和简历等。但是照片和简历都是文件,我发不出去。我得到以下错误:***“无法将JSON值转换为系统。Byte[]."***.
我该怎么解决?

public class DoctorRegisterDto
{
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public string Password { get; set; }
    public string Idnumber { get; set; }
    public string Email { get; set; }
    public int Views { get; set; }
    public string Categories { get; set; }
    public string Description { get; set; }
    public byte[] Photo { get; set; }
    public byte[] Cv { get; set; }
}
public class Doctor
{
    public int Id { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public byte[] PasswordHash { get; set; }
    public byte[] PasswordSalt { get; set; }
    public string Idnumber { get; set; }
    public string Email { get; set; }
    public string Categories { get; set; }
    public int Views { get; set; }
    public string Description { get; set; }
    public byte[] Photo { get; set; }
    public byte[] Cv { get; set; }
}


    [HttpPost("register-doctor")]
    public async Task<ActionResult<UserDto>> Register([FromBody] DoctorRegisterDto registerDto)
    {
        using var hmac = new HMACSHA512();

        var user = new Doctor
        {
            Firstname = registerDto.Firstname.ToLower(),
            Lastname = registerDto.Lastname.ToLower(),
            Idnumber = registerDto.Idnumber,
            Email = registerDto.Email.ToLower(),
            PasswordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(registerDto.Password)),
            PasswordSalt = hmac.Key,
            Categories = registerDto.Categories,
            Views = 0,
            Description = registerDto.Description,
            Photo = registerDto.Photo,
            Cv = registerDto.Cv
        };
        await _context.Doctors.AddAsync(user);
        await _context.SaveChangesAsync();

        return new UserDto { Username = user.Firstname, Token = _tokenService.CreateTokenDoctor(user) };
    }
gk7wooem

gk7wooem1#

如果你是通过表单发送的,试着将类型从byte[]改为IFormFile。

相关问题