ASP.NET MVC 5中的文件上载

时间:2015-05-26 11:52:21

标签: asp.net asp.net-mvc asp.net-mvc-5

我无法上传文件夹中的文件。我无法找到错误。 UploadFile View在上传文件后返回相同的视图。

模特课程:

public class Upload
    {
        public int UploadId { get; set; }
        public string UploadTitle { get; set; }
        public string UploadURL { get; set; }

    }

这是Controller(FileUpload)动作:

public ActionResult UploadFile(HttpPostedFileBase file, Upload upload)
        {
            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    string fil = System.IO.Path.GetFileName(file.FileName);
                    string path = System.IO.Path.Combine(Server.MapPath("/Content/Uploads/Files"), fil);
                    file.SaveAs(path);
                    upload.UploadURL = "/Content/Uploads/Files/" + file.FileName;
                }
                db.Uploads.Add(upload);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(upload);
        }

在我的观点中:

@using (Html.BeginForm("UploadFile, "FileUpload", FormMethod.Post, new { enctype = "multipart/Form-data" }))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">

        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            <div class="control-label col-md-2">
                <label for="file">Upload Image  for Slide:</label>
            </div>
            <div class="col-md-10">
                <input type="file" name="file" id="file" style="width:50%" />
            </div>

        </div>



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

2 个答案:

答案 0 :(得分:4)

您好,我已经尝试过相同的代码,但对我而言。

<强>控制器

   [HttpPost]
        public ActionResult UploadFile(HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    string fil = System.IO.Path.GetFileName(file.FileName);
                    string path = System.IO.Path.Combine(Server.MapPath("/Content/Uploads/Files"), fil);
                    file.SaveAs(path);
                }
                return RedirectToAction("Index");
            }

            return View("UploadFile");
        }

查看

@using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new { enctype = "multipart/Form-data" }))
{
@Html.AntiForgeryToken()

<div class="form-horizontal">

    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    <div class="form-group">
        <div class="control-label col-md-2">
            <label for="file">Upload Image  for Slide:</label>
        </div>
        <div class="col-md-10">
            <input type="file" name="file" id="file" style="width:50%" />
        </div>

    </div>



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

我在动作名称"的Html.BeginForm代码中发现了小错误(缺少双引号)

答案 1 :(得分:-1)

我忘记在上面的模型类中提到UploadURL上的必填字段:

Set<A<? extends B, ?>> attributes;

UploadURL字段上的必填字段验证限制了此处的文件上载。我从字段中删除了必填字段验证。