C# 带有模型的mvc上载文件-第二个参数发布的文件为空
我有一个简单的模型,带有一个字符串属性,我在一个简单的视图上渲染它 该视图如下所示:C# 带有模型的mvc上载文件-第二个参数发布的文件为空,c#,asp.net,asp.net-mvc,asp.net-mvc-3,razor,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 3,Razor,我有一个简单的模型,带有一个字符串属性,我在一个简单的视图上渲染它 该视图如下所示: @using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new { encType="multipart/form-data" })) { @Html.TextBoxFor(m => m.FirstName) <br /><br /> <input type="file" name=
@using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new { encType="multipart/form-data" }))
{
@Html.TextBoxFor(m => m.FirstName)
<br /><br />
<input type="file" name="fileUpload" /><br /><br />
<input type="submit" value="submit me" name="submitme" id="submitme" />
}
现在,当我提交时,确实填充了模型,但是第二个参数HttpPostedFileBase为null。但是,在执行Request.Files时,它似乎确实显示正在发布的请求中有一个文件。
如何实际获取要绑定的第二个参数 为什么不像这样将上传的文件添加到您的模型中:
public class UploadFileModel
{
public UploadFileModel()
{
Files = new List<HttpPostedFileBase>();
}
public List<HttpPostedFileBase> Files { get; set; }
public string FirstName { get; set; }
// Rest of model details
}
将您的姓名
文件
更改为文件上传
和加密类型
即可
@using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new { enctype="multipart/form-data" }))
{
@Html.TextBoxFor(m => m.FirstName)
<br /><br />
<input type="file" name="fileUpload" /><br /><br />
<input type="submit" value="submit me" name="submitme" id="submitme" />
}
[HttpPost]
public ActionResult UploadFile(UploadFileModel model, HttpPostedFileBase fileUpload)
{
// DO Stuff
return View(model);
}
@使用(Html.BeginForm(“UploadFile”,“Home”,FormMethod.Post,new{enctype=“multipart/formdata”}))
{
@Html.TextBoxFor(m=>m.FirstName)
}
[HttpPost]
公共操作结果上传文件(上传文件模型,HttpPostedFileBase文件上传)
{
//做事
返回视图(模型);
}
要处理单个文件输入,可以在ViewModel中定义HttpPostedFileBase
属性:
然后按以下方式实施:
查看:
[HttpPost]
public ActionResult Submit(SomeModel model)
{
// do something with model.SomeFile
return View();
}
@模型
@using (Html.BeginForm(
"Submit",
"Home",
FormMethod.Post,
new { enctype="multipart/form-data" }))
{
@Html.TextBoxFor(m => m.SomeFile, new { type = "file" })
<input type="submit" value="Upload"
name="UploadButton" id="UploadButton" />
}
如果需要处理多个文件,您可以:
- 创建多个属性,并像上面一样分别实现它们李>
- 将
属性更改为类似于public HttpPostedFileBase SomeFile
的属性,然后跨越多个public List SomeFiles
控件,使它们都位于该列表中@Html.TextBoxFor(m=>m.SomeFile,new{type=“file”})
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ActionWithFileUpload(ViewModel viewModel)
{
if (ModelState.IsValid)
{
if (Request.Files.Count > 0)
{
var postedFile = Request.Files[0];
if (postedFile != null && postedFile.ContentLength > 0)
{
string imagesPath = HttpContext.Server.MapPath("~/Content/Images"); // Or file save folder, etc.
string extension = Path.GetExtension(postedFile.FileName);
string newFileName = $"NewFile{extension}";
string saveToPath = Path.Combine(imagesPath, newFileName);
postedFile.SaveAs(saveToPath);
}
}
else
{
ModelState.AddModelError(string.Empty, "File not selected.");
}
}
return RedirectToAction("Index"); // Or return view, etc.
}
[HttpPost]
[ValidateAntiForgeryToken]
带有文件上载的公共异步任务操作(ViewModel ViewModel)
{
if(ModelState.IsValid)
{
如果(Request.Files.Count>0)
{
var postedFile=Request.Files[0];
if(postedFile!=null&&postedFile.ContentLength>0)
{
字符串imagesPath=HttpContext.Server.MapPath(“~/Content/Images”);//或文件保存文件夹等。
字符串扩展名=Path.GetExtension(postedFile.FileName);
字符串newFileName=$“NewFile{extension}”;
字符串saveToPath=Path.Combine(imagesPath,newFileName);
SaveAs(saveToPath);
}
}
其他的
{
AddModelError(string.Empty,“未选择文件”);
}
}
return RedirectToAction(“Index”);//或return view等。
}
很有趣。这起作用了。谢谢但是仍然没有理由不绑定到第二个参数。不过我想解释一下。我只是在等待它让我接受答案——一切都完成了。我同意你的看法,虚拟机方法。只是。。。请记住为什么它不会绑定第二个参数。为什么使用HttpPostedFileBase而不是HttpPostedFile?引用m.Files.Files是不正确的。如果要选择多个具有相同输入控件的文件(m=>m.Files,new{type=“file”,multiple=“multiple”,name=“Files”}),则需要像这样添加HTML5多属性@Ahmedilyas我不知道为什么它对您不起作用。它对我非常有效。这种方法正是我所寻找的,直接方法,我的问题解决方案中缺少的部分是BeginForm:-/“enctype=”multipart/form data“}”。绑定是按名称进行的,因为输入标记上有name=“fileUpload”
,您的操作方法的第二个参数的名称应相同,即HttpPostedFileBase fileUpload
。
public class SomeModel()
{
public SomeModel()
{
}
public HttpPostedFileBase SomeFile { get; set; }
}
@using (Html.BeginForm(
"Submit",
"Home",
FormMethod.Post,
new { enctype="multipart/form-data" }))
{
@Html.TextBoxFor(m => m.SomeFile, new { type = "file" })
<input type="submit" value="Upload"
name="UploadButton" id="UploadButton" />
}
[HttpPost]
public ActionResult Submit(SomeModel model)
{
// do something with model.SomeFile
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ActionWithFileUpload(ViewModel viewModel)
{
if (ModelState.IsValid)
{
if (Request.Files.Count > 0)
{
var postedFile = Request.Files[0];
if (postedFile != null && postedFile.ContentLength > 0)
{
string imagesPath = HttpContext.Server.MapPath("~/Content/Images"); // Or file save folder, etc.
string extension = Path.GetExtension(postedFile.FileName);
string newFileName = $"NewFile{extension}";
string saveToPath = Path.Combine(imagesPath, newFileName);
postedFile.SaveAs(saveToPath);
}
}
else
{
ModelState.AddModelError(string.Empty, "File not selected.");
}
}
return RedirectToAction("Index"); // Or return view, etc.
}