0

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?

Nikas Žalias
  • 1,594
  • 1
  • 23
  • 51
  • 1
    Possible duplicate of [Store X509 Certificate in database](http://stackoverflow.com/questions/4933759/store-x509-certificate-in-database) – Neo Oct 19 '16 at 17:31

1 Answers1

2

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();
    }
Mike_G
  • 16,237
  • 14
  • 70
  • 101