文件未上传到目录

时间:2014-05-19 13:10:16

标签: c# asp.net-mvc entity-framework

我正在尝试使用BeginForm()将文件上传到文件夹。我正在创建一个脚手架项目,我的观点如下:

@model Blog.Models.Resource

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

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

    <div class="form-horizontal">
        <h4>Resource</h4>
        <hr />
        @Html.ValidationSummary(true)

        <!---Name-->
        <div class="form-group">
            @Html.LabelFor(model => model.ResourceName, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ResourceName)
                @Html.ValidationMessageFor(model => model.ResourceName)
            </div>
        </div>

        <!---Resource-->
        <div class="form-group">
            @Html.LabelFor(model => model.Item, new { @class = "control-label col-md-2" })
        </div>
        <input type="file" name="FileUpload1" /><br />


        <!---Text-->
        <div class="form-group">
            @Html.LabelFor(model => model.Text, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextAreaFor(model => model.Text)
                @Html.ValidationMessageFor(model => model.Text)
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

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

我的模型如下:

  public class Resource
    {

        [Key]
        public int ResourceId { get; set; }

        public string ResourceName { get; set; }

        public string Item { get; set; }

        public string Text { get; set; }

    }

我的行动如下:

 // GET: /Resources/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: /Resources/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Resource resource, HttpPostedFileBase file)
        {
            //verify file that file exist
            if (file != null && file.ContentLength > 0)
            {
                //get filename
                var fileName = Path.GetFileName(file.FileName);
                //store file in speficied folder
                var path = Path.Combine(Server.MapPath("~/Catalog/Uploads"), fileName);
                file.SaveAs(path);
            }


            if (ModelState.IsValid)
            {
                db.Resources.Add(resource);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(resource);
        }

我一直在寻找其他帖子,我的情况几乎和这篇文章中描述的一样,但我确实包括{enctype = "multipart / form-data"}。但对我来说它不起作用:

ASP.NET MVC File-upload

当其他数据保存到数据库时,文件不会上传到指定的文件夹。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

您的文件未被保存会让我相信它是空的。我会尝试两件事:

  1. enctype上没有空格。所以它会是multipart/form-data
  2. 使文件上传的名称与您操作中的HttpPostedFileBase参数相匹配。因此,将<input type="file" name="FileUpload1" />更改为<input type="file" name="file" />。这可以帮助模型绑定器完成它的工作。

答案 1 :(得分:0)

您的问题是因为输入文件的nameHttpPostedFileBase变量名称不匹配。 所以你应该有这样的东西:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Resource resource, HttpPostedFileBase FileUpload1)
    {
        //verify file that file exist
        if (fFileUpload1 != null && FileUpload1.ContentLength > 0)
        {
            //get filename
            var fileName = Path.GetFileName(FileUpload1.FileName);
            //store file in speficied folder
            var path = Path.Combine(Server.MapPath("~/Catalog/Uploads"), fileName);
           FileUpload1.SaveAs(path);
        }


        if (ModelState.IsValid)
        {
            db.Resources.Add(resource);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(resource);
    }