Nikas ┼Żalias Nikas ┼Żalias - 4 months ago 76
ASP.NET (C#) Question

ASP.NET MVC upload certificate

what is correct way to upload X509Certificate (.cer) content to database from form in View?

In my View I have this upload input:

<div class="form-group row">
<label asp-for="Certificates" class="col-sm-2 col-sm-offset-2 form-control-label"></label>
<input type="file" asp-for="Certificates"/>

In my ViewModel I have this parameter:

public IFormFile Certificate { get; set; }

My controller gets this IFormFile, but I can't get certificate's content in byte[]. How could I get this byte array by uploading certificate?


use an action that accepts an IFormFile parameter.

    public async Task<IActionResult> UploadSomeFile(IFormFile file){

        byte[] bytes = new byte[file.Length];
        using (var reader = file.OpenReadStream())
            await reader.ReadAsync(bytes, 0, (int)file.Length);
         //send bytes to db

        return Ok();