简单的文件上传返回NULL? [英] Simple file upload returns NULL?

查看:90
本文介绍了简单的文件上传返回NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做我的第一个简单的文件上传在MVC 5.我下了一堆我发现的例子,但由于某种原因,在我的创建的ActionResult的uploadFile总是进来为NULL所以上传code从不运行。任何人看到我可能是做错了?

  @ {
    ViewBag.Title =创建;
    布局=〜/查看/共享/ _Layout.cshtml
}
< H2>文档和LT; / H2>
< H4方式>上传一个新的文档和LT; / H4>< D​​IV CLASS =以及>
@using(Html.BeginForm(创建,文档,FormMethod.Post,新{ENCTYPE =的multipart / form-data的}))
{
    < H3>选择要上传的文件。 < / H3 GT&;    <输入类型=文件名称=文件VALUE =多个=多​​/>    <输入类型=提交值=上传文件称号=上传/>    < D​​IV的风格=颜色:红色; FONT-SIZE:14px的> @ ViewBag.Message< / DIV>
}
< / DIV>

下面是我的控制器:

  // POST:文件/新建
        [HttpPost]
        公众的ActionResult创建(HttpPostedFileBase uploadFile)
        {
            尝试
            {
                如果(uploadFile =空&放大器;!&放大器; uploadFile.ContentLength大于0)
                {                    字符串文件路径=使用Server.Mappath(../ SiteDocuments+ uploadFile.FileName);                    uploadFile.SaveAs(文件路径);
                }                返回RedirectToAction(「指数」);
            }
            赶上(异常前)
            {
                返回查看();
            }        }


解决方案

您文件输入元素的名称应与你的操作方法参数的名称。

所以更新您的HTML标记具有相同的名称属性值。

 <输入类型=文件名称=一个UploadFileVALUE =多个=多​​/>

和动作方法将是

  [HttpPost]
公众的ActionResult创建(HttpPostedFileBase uploadFile)
{
  // 做一点事
}

改变你的操作方法参数的名称,以配合您的文件输入元素名称。

 <输入类型=文件名称=文件VALUE =多个=多​​/>

和动作方法将是

  [HttpPost]
公众的ActionResult创建(HttpPostedFileBase文件)
{
    如果(文件= NULL&放大器;!&安培; files.ContentLength大于0)
    {
       // 做一点事
    }
}

当您添加多=多个属性的input元素,浏览器将允许最终用户一次选择多个文件。在这种情况下,如果你的动作方法的参数是 HttpPostedFileBase 对象的一个​​实例,它会收到从选定 N 文件。如果你想要的所有文件,你可以改变你的参数集合,如

  [HttpPost]
公众的ActionResult创建(IEnumerable的< HttpPostedFileBase>文件)
{
    如果(文件= NULL&放大器;!&安培; files.Any())
    {
        的foreach(在文件var文件)
        {
            如果(file.ContentLength大于0)
            {
                //做一点事
            }
        }
    }
}

I'm trying to do my first simple file upload in MVC 5. I'm following a bunch of examples I've found but for some reason in my "Create" ActionResult the uploadFile is always coming in as NULL so the upload code is never running. Anyone see what I might be doing wrong?

@{
    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_Layout.cshtml";
}


<h2>Documents.</h2>
<h4>Upload a new document.</h4>

<div class="well">
@using (Html.BeginForm("Create", "Documents", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <h3>Select a file to upload. </h3>

    <input type="file" name="files" value="" multiple="multiple" />

    <input type="submit" value="Upload your file" title="Upload" />

    <div style="color:Red;font-size:14px">@ViewBag.Message</div>
}
</div>

Here is my controller:

// POST: Documents/Create
        [HttpPost]
        public ActionResult Create(HttpPostedFileBase uploadFile)
        {
            try
            {
                if(uploadFile != null && uploadFile.ContentLength > 0)
                {

                    string filePath = Server.MapPath("../SiteDocuments" + uploadFile.FileName);

                    uploadFile.SaveAs(filePath);
                }

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return View();
            }

        }

解决方案

Your file input element's name should match to your action method parameter name.

So update your HTML markup to have the same name attribute value.

<input type="file" name="uploadFile" value="" multiple="multiple" />

and your action method will be

[HttpPost]
public ActionResult Create(HttpPostedFileBase uploadFile)
{
  // do something
}

Or change your action method parameter name to match with your file input element name.

<input type="file" name="files" value="" multiple="multiple" />

and your action method will be

[HttpPost]
public ActionResult Create(HttpPostedFileBase files)
{
    if(files!= null && files.ContentLength > 0)
    {
       // do something
    }
}

When you add multiple="multiple" attribute to the input element, the browser will allow the end user to select more than one file at a time. In that case If your action method parameter is a single instance of HttpPostedFileBase object, It will receive the first file from the selected n files. If you want all the files, You may change your parameter to a collection such as

[HttpPost]
public ActionResult Create(IEnumerable<HttpPostedFileBase> files)
{
    if (files != null && files.Any())
    {
        foreach (var file in files)
        {
            if (file.ContentLength > 0)
            {
                //do something
            }
        }
    }
}

这篇关于简单的文件上传返回NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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