使用ENCTYPE =&QUOT时多部分/格式数据和QUOT;窗体不会发布 [英] when using enctype="multipart/form-data" form doesn't post
问题描述
我工作在ASP.Net MVC 4应用程序,我试图在窗体中上传图片。我遇到的问题是,当窗体的职位,如果<输入类型=文件...
为空(意思是我没有选择一个文件)形式帖子就好了,一切工作。但是,当我做选择一个文件的形式只是坐在那里,什么都不做。
I'm working on an app on ASP.Net MVC 4 and am trying to upload an image in a form. The problem I'm having is that when the form posts, if the <input type="file"...
is empty (meaning I have not selected a file) the form posts just fine and everything works. However, When I do select a file the form just sits there and does nothing.
起初我还以为这只是把它一段时间才能上传,但我已经离开了很长一段时间(文件大小为7KB)并没有什么。调试器不即使在动作开始时打断点任一。我在一个损失,因为我很新的平台,我每天还在学习。下面请找出所有相关的code:
At first I thought it was just taking it a while to upload but I have left it for quite some time (file size was 7kb) and nothing. the debugger doesn't even hit the breakpoints at the beginning of the action either. I'm at a loss since I'm quite new to the platform and am still learning every day. Below please find all the relevant code:
查看:
@using (Html.BeginForm("StaffInformation", "Manager", FormMethod.Post, new { @class = "form-horizontal", enctype = "multipart/form-data"}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="control-group">
@Html.LabelFor(model => model.firstName, new { @class = "control-label" })
<div class="controls">
@Html.TextBoxFor(model => model.firstName, new { @class = "span12" })
@Html.ValidationMessageFor(model => model.firstName)
</div>
</div>
<div class="control-group">
@Html.LabelFor(model => model.lastName, new { @class = "control-label" })
<div class="controls">
@Html.TextBoxFor(model => model.lastName, new { @class = "span12" })
@Html.ValidationMessageFor(model => model.lastName)
</div>
</div>
<div class="control-group">
Staff Photo:
<div class="controls">
@if (Model.staffImage == null)
{
@:None
}else{
<img width="150" height="150" src="@Url.Action("GetImage", "Manager", new { Model.staffProfileID })" /><br />
}
<input type="file" id="staffImage" name="staffImage" data-provide="fileinput">
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Save changes</button>
<a href="@Url.Action("dashboard", "manager")" class="btn" type="reset" >Cancel</a>
</div>
}
该行动:
public ActionResult StaffInformation(StaffInformationViewModel model, HttpPostedFileBase staffImage)
{
if (ModelState.IsValid) //This is where the breakpoint is
{
if (staffImage != null) {
model.imageMimeType = staffImage.ContentType;
model.staffImage = new byte[staffImage.ContentLength];
staffImage.InputStream.Read(model.staffImage, 0, staffImage.ContentLength);
}
using (var repo = new CompanyInformationRepository(new UniteOfWorkCompanies()))
{
var company = repo.FindCompany(User.Identity.Name);
repo.AddOrUpdateStaff(model, company);
}
return RedirectToAction("ManageStaff");
}
}
我真的不知道怎么回事,只是我使用 ENCTYPE =的multipart / form-data的
的原因是因为我读了临ASP.NET MVC 4由亚当·弗里曼,并在那里,他们说,它不会没有它的工作。就像我说的,我很新的,需要各方面的帮助,我可以得到的。
I really don't know whats going on and simply the reason I'm using the enctype = "multipart/form-data"
is because I was reading the "Pro ASP.NET MVC 4" by Adam Freeman and in there they said that it won't work without it. Like I said, I'm quite new and need all the help I can get.
推荐答案
您可以尝试把 HttpPostedFileBase staffImage
为您的视图模型(的一部分StaffInformationViewModel
) - 还不错,你需要保持 ENCTYPE =的multipart / form-data的
的形式标记:
You can try to put the HttpPostedFileBase staffImage
as part of your ViewModel (StaffInformationViewModel
) - also yes, you need to keep the enctype = "multipart/form-data"
in the form tag:
public class StaffInformationViewModel
{
// your other properties here ...
[Required]
public HttpPostedFileBase StaffImage { get; set; }
}
然后在视图:
@Html.TextBoxFor(model => model.StaffImage, new { type = "file" })
然后在控制器:
public ActionResult StaffInformation(StaffInformationViewModel model)
{
if (ModelState.IsValid)
{
if (model.StaffImage != null)
{
// awesome
}
// ... more awesome
}
}
我希望这有助于。让我知道你在这里需要在这一个多澄清...
I hope this helps. Let me know if you need more clarifications here on this one ...
这篇关于使用ENCTYPE =&QUOT时多部分/格式数据和QUOT;窗体不会发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!