我试图将图像上传到我的App_Data文件夹中。我使用了HttpPostedFileBase,但由于某种原因它总是返回null。 这里是我的创建方法:MVC获取图像的NULL路径
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult mkCreate(HttpPostedFileBase file)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data"), fileName);
file.SaveAs(path);
}
return View();
}
这是我的看法(Create.cshtml):
@using (Html.BeginForm("mkCreate", "Resim", FormMethod.Post, new { enctype= "multipart/form-data" }))
{
<table>
<tr>
<td>Image:</td>
<td><input type="file" name="Images" id="Images" multiple /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Upload" /></td>
</tr>
</table>
}
能否请你帮我上传图片到我的App_Data文件夹? 在此先感谢。
这是我尝试后得到的结果:“所需的防伪表单字段”__RequestVerificationToken“不存在” –
哦,因为您在控制器上使用了validateantiforryry标记行动,你必须在你的表单上使用@ Html.AntiForgeryToken()。请再次检查编辑的代码。 – InsParbo
谢谢你!有效! –