我只是尝试了这个,但它不起作用,它有什么问题,
我的索引页:
@{
ViewBag.Title = "Index";
}
@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/from- data" }))
{
<div>
<h1 style="align-content: center; color: blueviolet">Application to upload files</h1>
</div>
<div>
<input type="file" id="file" name="file" />
<br />
<input type="submit" id="load" name="submit" value="Submit" />
</div>
}
我的控制器是,
[HttpPost]
public ActionResult Upload()
{
string path = @"~/Content/Upload";
HttpPostedFileBase file = Request.Files["file"];
if (file != null)
file.SaveAs(path + file.FileName);
return Content("Sucess");
}
答案 0 :(得分:1)
您尝试将文件保存为错误的路径。试试MapPath:
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
string path = Server.MapPath("~/Content/Upload");
if (file != null)
{
file.SaveAs(Path.Combine(path, file.FileName));
}
return Content("Sucess");
}
另外,请确保您在表单中使用了正确的enctype
属性:
enctype = "multipart/form-data"
而不是:
enctype = "multipart/from- data"