图像文件数据不属于我的控制器。我发送的文件类型为IEnumerable<HttpPostedFileBase>
。
这是我的观点
@using (Html.BeginForm( new { @action = "/Upload", @enctype = "multipart/form-data", @method = "POST" }))
{
<div class="form-horizontal">
<div class="form-group">
<div class="col-md-10">
@Html.Label("Pictures:", new { @class = "control-label col-md-2" })
@Html.TextBoxFor(model => model.files, new { @type = "file", @name = "files", @id = "files", @style = "width: 100%;", @multiple = "multiple" })
</div>
</div>
<div id="divfiles"></div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script>
function handleFileSelect(evt) {
var files = evt.target.files;
var divfiles = document.getElementById('divfiles');
while (divfiles.hasChildNodes()) {
divfiles.removeChild(divfiles.lastChild);
}
for (var i = 0, f; f = files[i]; i++) {
if (!f.type.match('image.*')) {
continue;
}
var reader = new FileReader();
reader.onload = (function(theFile) {
return function(e) {
var span = document.createElement('span');
var imgLoad = document.createElement('img');
imgLoad.setAttribute("id", "imgLoad");
imgLoad.setAttribute("style", "width:10%;");
imgLoad.setAttribute("src", e.target.result);
imgLoad.setAttribute("title", escape(theFile.name));
span.appendChild(imgLoad);
divfiles.insertBefore(span, null);
};
})(f);
reader.readAsDataURL(f);
}
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>
}
这是我的控制器
这里的图像不是
// GET:
public ActionResult Index()
{
var rooms = db.Rooms.Include(r => r.Hotel).Include(r => r.RoomType);
return View(rooms.ToList());
}
// GET: Rooms/Details/
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Room room = db.Rooms.Find(id);
if (room == null)
{
return HttpNotFound();
}
return View(room);
}
// GET: Rooms/Create
public ActionResult Create()
{
RoomViewModel model = new RoomViewModel();
model.HotelIDs = from c in db.Hotels
select new SelectListItem
{
Text = c.Name,
Value = c.Id.ToString()
};
model.RoomTypeIDs = from t in db.RoomTypes
select new SelectListItem
{
Text = t.Name,
Value = t.Id.ToString()
};
model.EquipmentIDs = from k in db.Equipments
select new SelectListItem
{
Text = k.Name,
Value = k.Id.ToString()
};
//ViewBag.HotelId = new SelectList(db.Hotels, "Id", "Name");
//ViewBag.RoomTypeId = new SelectList(db.RoomTypes, "Id", "Name");
//ViewBag.EquipmentsIds = new MultiSelectList(db.Equipments, "Id", "Name");
return View(model);
}
// POST: Rooms/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(IEnumerable<HttpPostedFileBase> files, RoomViewModel roomViewModel, int[]EquipmentIDs )
{
Room room = new Room();
room.HotelId = roomViewModel.HotelId;
room.RoomTypeId = roomViewModel.RoomTypeId;
room.MaxPeople = roomViewModel.MaxPeople;
room.WindowView = roomViewModel.WindowView;
room.Price = roomViewModel.Price;
room.IsDeleted = roomViewModel.IsDeleted;
if (EquipmentIDs == null)
{
foreach (int EquipmentID in EquipmentIDs)
{
var eqp = db.Equipments.Where(t => t.Id == EquipmentID).FirstOrDefault();
if (eqp != null)
{
if (room.Equipments == null)
{
room.Equipments = new List<Equipment>();
}
room.Equipments.Add(eqp);
}
}
}
//room.RoomsPhotos = roomViewModel.files;
if (ModelState.IsValid)
{
db.Rooms.Add(room);
db.SaveChanges();
}
return RedirectToAction("Index");
}
[HttpPost]
public ActionResult Upload(IEnumerable<HttpPostedFileBase> files)
{
return View("Index", "Hotels");
}
方法上传文件(IEnumerable文件)没有。
答案 0 :(得分:1)
您的代码未生成正确的form
标记。要使文件上传生效,您的表单应将enctype
属性值设置为"multipart/form-data"
您当前的视图代码正在呈现这样的表单标记(检查页面的查看源)
<form action="/Upload?enctype=multipart%2Fform-data&method=POST" method="post">
</form>
但是为了上传工作,理想情况下应该是
<form action="/Upload" enctype="multipart/form-data" method="post">
</form>
如果您正确使用Html.BeginForm
辅助方法,它将生成正确的表单标记标记。
这应该有效
@using (Html.BeginForm("Upload", "Home", FormMethod.Post,
new { @enctype = "multipart/form-data" }))
{
}
假设您的Upload
操作方法位于HomeController
。如果它是不同的控制器,请更新Html.BeginForm method
呼叫的第二个参数。
答案 1 :(得分:0)
[这不是答案,而是长篇评论和建议]
如果IEnumerable<HttpPostedFileBase>
无效,请试试这个!!!
[HttpPost]
public ActionResult Upload()
{
var file = request.files;
}