我正在尝试使用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"}
。但对我来说它不起作用:
当其他数据保存到数据库时,文件不会上传到指定的文件夹。
有人可以帮忙吗?
答案 0 :(得分:0)
您的文件未被保存会让我相信它是空的。我会尝试两件事:
enctype
上没有空格。所以它会是multipart/form-data
。 HttpPostedFileBase
参数相匹配。因此,将<input type="file" name="FileUpload1" />
更改为<input type="file" name="file" />
。这可以帮助模型绑定器完成它的工作。答案 1 :(得分:0)
您的问题是因为输入文件的name
与HttpPostedFileBase
变量名称不匹配。
所以你应该有这样的东西:
[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);
}