简体   繁体   English

MVC4 Razor中文件上传中的Null异常

[英]Null Exception in File Uploading in MVC4 Razor

I have created the following view, with having file upload and submit button. 我创建了以下视图,并具有文件上传和提交按钮。

@using (Html.BeginForm("FileUpload", "Home",
                    FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input name="uploadFile" type="file" />
    <input type="submit" value="Upload File" id="btnSubmit" />
}

I have also created the action method in Controller but it gives the null at the "uploadFile" 我还在Controller中创建了action方法,但它在“ uploadFile”处提供了null

[HttpPost)]
        public ActionResult FileUpload(HttpPostedFileBase uploadFile)
        {
            if (uploadFile.ContentLength > 0)
            {
                string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"),
                                               Path.GetFileName(uploadFile.FileName));
                uploadFile.SaveAs(filePath);
            }
            return View();
        }

Can u try Name with same as uploadFile 您可以尝试使用与uploadFile相同的Name

In your Page: 在您的页面中:

@using (Html.BeginForm("FileUpload", "Home",
                    FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input id="uploadFile" name="uploadFile" type="file" />
    <input type="submit" value="Upload File" id="btnSubmit" />
}

As per @Willian Duarte comment : [HttpPost] 按照@Willian Duarte的评论: [HttpPost]

In your Code behind: 在后面的代码中:

[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase uploadFile) // OR IEnumerable<HttpPostedFileBase> uploadFile
{
    //For checking purpose 
     HttpPostedFileBase File = Request.Files["uploadFile"];

    if (File != null)
    {
        //If this is True, then its Working.,
    }

    if (uploadFile.ContentLength > 0)
    {
        string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"),
                                       Path.GetFileName(uploadFile.FileName));
        uploadFile.SaveAs(filePath);
    }
    return View();
}

See here Same question like yours., 看到这里与您一样的问题。

Code project Article about the File upload., 关于文件上传的代码项目文章。

create a model and bind it to your view that controller will also expect: 创建一个模型并将其绑定到控制器还将期望的视图:

Controller: 控制器:

    //Model (for instance I've created it inside controller, you can place it in model
    public class uploadFile
    {
        public HttpPostedFileBase file{ get; set; }
    }

    //Action
    public ActionResult Index(uploadFile uploadFile)
    {
        if (uploadFile.file.ContentLength > 0)
        {
            string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"),
                                           Path.GetFileName(uploadFile.file.FileName));
            uploadFile.file.SaveAs(filePath);
        }
        return View();
    }

View @model sampleMVCApp.Controllers.HomeController.uploadFile 查看 @model sampleMVCApp.Controllers.HomeController.uploadFile

@using (Html.BeginForm("FileUpload", "Home",
                FormMethod.Post, new { enctype = "multipart/form-data" }))
{
  @Html.TextBoxFor(m => m.file, new { type = "file"});  
 <input type="submit" value="Upload File" id="btnSubmit" />
}

Tested Solution! 经过测试的解决方案!

HTH :) HTH :)

尝试使用(在控制器上):

var file = System.Web.HttpContext.Current.Request.Files[0];

Use the following in the controller: 在控制器中使用以下命令:

var file = System.Web.HttpContext.Current.Request.Files[0];

Use HttpPost instead of [AcceptVerbs(HttpVerbs.Post)] 使用HttpPost代替[AcceptVerbs(HttpVerbs.Post)]

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

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