【问题标题】:ASP.NET MVC upload certificateASP.NET MVC 上传证书
【发布时间】:2016-10-19 16:44:02
【问题描述】:

将 X509Certificate (.cer) 内容从 View 中的表单上传到数据库的正确方法是什么?

在我看来,我有这个上传输入:

 <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>

在我的 ViewModel 我有这个参数:

public IFormFile Certificate { get; set; }

我的控制器获得了这个 IFormFile,但我无法在字节 [] 中获得证书的内容。如何通过上传证书得到这个字节数组?

【问题讨论】:

标签: asp.net asp.net-mvc asp.net-core x509certificate .net-core-rc2


【解决方案1】:

使用接受 IFormFile 参数的操作。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-12
    • 2018-05-02
    • 2016-06-24
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    相关资源
    最近更新 更多