在MVC4中上传,我的操作中有2个参数,但文件为空

时间:2013-01-17 21:52:45

标签: forms file-upload upload asp.net-mvc-4 httppostedfilebase

我正在尝试将文件上传到目录。以下代码对我有用

CONTROLLER

[HttpPost]
        public ActionResult Index(HttpPostedFileBase uploadFile)
        {
            //string path = @"C:\Users\thomas\Desktop";

            if (uploadFile != null)
            {
                string filePath = Path.Combine(Server.MapPath("/files"), Path.GetFileName(uploadFile.FileName));
                uploadFile.SaveAs(filePath);
            }

            return RedirectToAction("Index");
        }

HTML PAGE

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
<form action="/Post/Index" method="post" enctype="multipart/form-data">
    <label for="uploadFile">Upload file: </label>
    <input name="uploadFile" id="uploadFile" type="file" />
    <input value="uploadFile" type="submit" />
</form>

现在我正在尝试在一个函数中实现它,我创建一个消息,该消息由包含消息和项类的模型创建。当我提交表单时,模型将传递给我的savecontroller,但文件在我的参数控制器中为空。

HTML PAGE

创建新消息

@model GeoCitytroopers.Models.MessageItemModel
@{
    ViewBag.Title = "Create";
}
@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Event</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Message.MessagePicture)
        </div>
        <div>
            <label for="uploadFile">Upload file: </label>
            <input name="uploadFile" id="uploadFile" type="file" />
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Message.MessagePicture)
            @Html.ValidationMessageFor(model => model.Message.MessagePicture)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Item.ItemTitle)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Item.ItemTitle)
            @Html.ValidationMessageFor(model => model.Item.ItemTitle)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Item.ItemDescription)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Item.ItemDescription)
            @Html.ValidationMessageFor(model => model.Item.ItemDescription)
        </div>

         <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

CONTROLLER

[HttpPost]
        public ActionResult Create(HttpPostedFileBase uploadFile, MessageItemModel ViewModel)
        {
            if (ModelState.IsValid)
            {
                Utility ut = new Utility();
                Item viewItem = ViewModel.Item;
                Message viewMessage = ViewModel.Message;

                if (uploadFile != null)
                {
                    string filePath = Path.Combine(Server.MapPath("/files"), Path.GetFileName(uploadFile.FileName));
                    uploadFile.SaveAs(filePath);
                }
                //ADD USER TO ITEM
                viewItem = ut.AddUserToItem(viewItem);

                //ADD ITEM
                viewItem.ItemCreateddate = DateTime.Now;


                //ADD DISTRICT TO ITEM
                viewItem.DistrictID = ut.GetUserDistrict();

                db.Items.Add(viewItem);

                //ADD LINK
                viewMessage.Item = viewItem;
                db.Messages.Add(viewMessage);

                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(ViewModel);
        }

如何将上传文件传递给控制器​​? 提前谢谢!

2 个答案:

答案 0 :(得分:6)

您忘了在表单中设置正确的enctype。如果没有,则无法上传文件:

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

现在上传工作正常,您的uploadFile参数不会为空。

答案 1 :(得分:2)

我最初的猜测是你使用Html助手创建的没有必要的加密。

尝试使用

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

}

具有action-namecontroller-name

的适当值