ASP.NET MVC upload certificate

847 Views Asked by At

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"/>
 </div>

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?

1

There are 1 best solutions below

0
Mike_G On BEST ANSWER

use an action that accepts an IFormFile parameter.

  [HttpPost]
    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();
    }