【问题标题】:asp.net mvc-5 HttpPostedFileBase is null [duplicate]asp.net mvc-5 HttpPostedFileBase 为空 [重复]
【发布时间】:2017-10-22 23:39:46
【问题描述】:

我正在使用 asp.net mvc-5 HttpPostedFileBase 进行文件上传,但在我选择图像后显示 HttpPostedFileBase 为空

这是我的代码

 <input type="file" title="search image" file-model="profileimage" id="allfilepath" name="file" />
 <button type="submit" class="btn btn-primary col-sm-5">
         <span class="glyphicon glyphicon-plus"></span>
 </button>

和我的控制器

    [HttpPost]
    public ActionResult insert(HttpPostedFileBase file, quotationcompany quotecomp)
    {
        var allowedExtensions = new[] {
        ".Jpg", ".png", ".jpg", "jpeg"
    };
        if (file == null)
        {
            ViewBag.message = "Please Select Image";
            return View();
        }
        else {
            ViewBag.message = "Image Uploaded Successfully";
            return View();
        }

    }

if (file == null)(在我上传 png 图像后,这一行 (file) 显示为 null)

这段代码有什么问题?

【问题讨论】:

  • 一个建议!请在发布重复问题之前使用Search,否则您的问题可能会被否决(因为您已经有 9 个问题),这可能会导致您Question Ban

标签: asp.net-mvc file-upload asp.net-mvc-5 httppostedfilebase


【解决方案1】:

检查表单属性

人们常犯的一个错误是表单标签中缺少以下部分:

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

同样在 MVC 中,您的表单结构可能看起来像这样

@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { @enctype = "multipart/form-data", @id = "myForm", @role="form" }))

【讨论】:

  • enctype="multipart/form-data" 是启用 HTML 文件上传支持的正确值。
  • 没错。已编辑。道歉。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-08
  • 2016-01-02
  • 2017-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-08
相关资源
最近更新 更多