asp.net mvc-5 HttpPostedFileBase is null [duplicate]

独自空忆成欢 提交于 2019-12-01 12:48:38

问题


i am messing around with file uploading using asp.net mvc-5 HttpPostedFileBase but it is showing me HttpPostedFileBase is null after i am selecting the image

here is my code

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

and my controller

    [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)(on this line (file) is showing me null after i am uploading png image)

what is wrong in this code?


回答1:


Check Form Attributes

A common mistake people make is missing the following part in the form tag:

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

Also in MVC your form structure could look something like this

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


来源:https://stackoverflow.com/questions/44127682/asp-net-mvc-5-httppostedfilebase-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!