简体   繁体   English

HttpPostedFileBase始终返回null

[英]HttpPostedFileBase always returns null

I am using MVC 4 and I tried for upload file concept. 我正在使用MVC 4,我尝试上传文件概念。

Here is my code: 这是我的代码:

<div class="complianceSubDiv">
  <div class="complianceLeftDiv">
    @Html.Label("Upload the file")
  </div>
  <div class="complianceRightDiv">
    <input type="file" id="file" name="file" />
  </div>
</div>

My controller code like 我的控制器代码就像

[HttpPost]
public ActionResult ManageDocument(DocumentModel documentModel, HttpPostedFileBase file)
{
    //some code
}

But the HttpPostedFileBase file always returns null. 但是HttpPostedFileBase文件始终返回null。 I have searched more answers in StackOverflow and other websites and I got the working answer is parameter of HttpPostedFileBase variable name and fileupload control name are same . 我在StackOverflow和其他网站上搜索了更多答案,我得到的工作答案是HttpPostedFileBase变量名参数和fileupload控件名相同 So I put the same name on all sides, but it returns null only. 所以我在所有方面都使用相同的名称,但它仅返回null

Anyone help to me? 有人帮我吗?

Finally i got it 终于我明白了

Now i replaced for @using (Html.BeginForm()) 现在我替换为@using (Html.BeginForm())

to

@using (Html.BeginForm("ManageDocument", "Document", FormMethod.Post, new { enctype = "multipart/form-data" }))

It's working ! 它的工作原理!

    [HttpPost]
    public ActionResult FileUpload(HttpPostedFileBase myFile)
    {
        myFile = Request.Files["file"];
        if (myFile != null && myFile.ContentLength > 0)
        {
            // your code ....
        }
        return View();
    }

You can use "Request.Files" to get the selected file, above is the code. 您可以使用“Request.Files”来获取所选文件,上面是代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM