Asp.net mvc MVC获取映像的空路径
我正在尝试将图像上载到我的App_数据文件夹。我使用了HttpPostedFileBase,但由于某些原因,它总是返回null。 以下是我的创建方法:Asp.net mvc MVC获取映像的空路径,asp.net-mvc,asp.net-mvc-4,razor,Asp.net Mvc,Asp.net Mvc 4,Razor,我正在尝试将图像上载到我的App_数据文件夹。我使用了HttpPostedFileBase,但由于某些原因,它总是返回null。 以下是我的创建方法: [HttpPost] [ValidateAntiForgeryToken] public ActionResult mkCreate(HttpPostedFileBase file) { if (file.ContentLength > 0) { var f
[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):
@使用(Html.BeginForm(“mkCreate”、“Resim”、FormMethod.Post、new{enctype=“multipart/formdata”}))
{
图片:
}
你能帮我把图片上传到我的App_数据文件夹吗?
提前感谢。尝试使用此添加类型
public ActionResult Create(HttpPostedFileBase file)
{
//Rest of the code
}
看法
@使用(Html.BeginForm(“YourMethod”,“Controller”,FormMethod.Post,new{enctype=“multipart/formdata”}))
{
}
您的图像模型可能是这样的:
public class Image
{
public IEnumerable<HttpPostedFileBase> Images { get; set; }
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Image image)
{
foreach (var file in image.Images)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data"), fileName);
file.SaveAs(path);
}
}
ViewBag.Message = "Image(s) uploaded successfully";
return View();
}
@model AppName.Models.Image
@{
ViewBag.Title = "Index";
}
<h2>Image Upload Test</h2>
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype "multipart/form-data" }))
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
{
<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>
}
最后,你的观点可能是这样的:
public class Image
{
public IEnumerable<HttpPostedFileBase> Images { get; set; }
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Image image)
{
foreach (var file in image.Images)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data"), fileName);
file.SaveAs(path);
}
}
ViewBag.Message = "Image(s) uploaded successfully";
return View();
}
@model AppName.Models.Image
@{
ViewBag.Title = "Index";
}
<h2>Image Upload Test</h2>
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype "multipart/form-data" }))
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
{
<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>
}
@model AppName.Models.Image
@{
ViewBag.Title=“Index”;
}
图像上传测试
@使用(Html.BeginForm(null,null,FormMethod.Post,new{enctype“multipart/formdata”}))
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
{
图片:
}
只需在Html.BeginForm()
中包含enctype
仍然为空:(你能更新你的代码吗..这样我们就可以看到你所做的一切了ok@Nilesh我做了这件事…只需将输入类型文件
名称更改为文件
这是我尝试后得到的结果:“必需的防伪表单字段”__RequestVerificationToken“不存在”哦,由于您在控制器操作上使用了validateantiforgery令牌,您也必须在表单上使用@Html.AntiForgeryToken()。请再次检查编辑的代码。