文件未上载到目录
本文关键字:上载 文件 | 更新日期: 2023-09-27 18:03:57
我试图用BeginForm()将文件上传到文件夹。我正在创建一个scaffold项目,我的视图如下所示:
@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; }
}
My Action如下:
// 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文件上传
当其他数据保存到数据库时,文件没有上传到指定的文件夹。
有人能帮忙吗?
你的文件没有被保存会让我相信它是空的。我想尝试两件事:
- 在
enctype
上不要尝试空格。所以是multipart/form-data
。 - 使您的文件上传的名称与您的操作中的
HttpPostedFileBase
参数匹配。因此将<input type="file" name="FileUpload1" />
更改为<input type="file" name="file" />
。这可以帮助模型绑定器完成工作。
您的问题是因为输入文件的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);
}