IFormFile始终为null(带有MVC/Razor的ASP.NET Core) [英] IFormFile always null (ASP.NET Core with MVC/Razor)

查看:73
本文介绍了IFormFile始终为null(带有MVC/Razor的ASP.NET Core)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET Core MVC应用程序尝试上载IFormFile.但是,IFormFile始终为null.我发现的其他解决方案均未解决此问题.我在做什么错了?

I have an ASP.NET Core MVC app attempting to upload an IFormFile. However, the IFormFile is always null. None of the other solutions I've found have solved this issue. What am I doing wrong?

型号

public class EmailForm
{
    [Display(Name = "Add a picture")]
    [DataType(DataType.Upload)]
    [FileExtensions(Extensions = "jpg,png,gif,jpeg,bmp,svg")]
    public IFormFile SubmitterPicture { get; set; }
}

控制器

public async Task<ActionResult> Contribute([Bind("SubmitterPicture")] EmailForm model)
{
    if (ModelState.IsValid)
    {
        //Do some stuff
    }
}

查看

        <form method="post" asp-action="Contribute" asp-antiforgery="true" enctype="multipart/form-data" >
            <div class="form-group" >
                <div class="col-md-2">@Html.LabelFor(m => m.SubmitterPicture)</div>
                <div class="col-md-4">
                    <input type="file" name="SubmitterPicture" id="SubmitterPicture" />
    </div>
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" class="btn btn-default" value="Contribute" />
                </div>
            </div>

推荐答案

<form method="post" enctype="multipart/form-data">
</form>

enctype ="multipart/form-data"

enctype="multipart/form-data"

多部分表单数据是关键.

The multipart form data is the key.

这篇关于IFormFile始终为null(带有MVC/Razor的ASP.NET Core)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆