Request.Files - 获取第一个没有foreach循环的File

时间:2015-05-14 07:48:48

标签: c# asp.net-mvc

我是网络新手。这是我的SELECT * FROM `nhrd_members` B WHERE Membership_Number LIKE 'A%' AND `Member_FromDate` >= '2014-01-01' AND `Member_FromDate` <= '2015-01-01' where id not in (SELECT id FROM `nhrd_members` A WHERE Membership_Number LIKE 'A%' AND `Member_FromDate` >= '2014-01-01' AND `Member_FromDate` <= '2014-05-14');

action

问题:我知道在Request.Files中只能有一个文件。是否有任何方法可以在没有[HttpPost] public virtual ActionResult SaveFile(IEnumerable<VacationSchedule.Models.VacationTypeViewModel> vacationTypes) { foreach (string fileName in Request.Files) { HttpPostedFileBase file = Request.Files[fileName]; string type = file.ContentType; string nameAndLocation = "~/Documents/" + System.IO.Path.GetFileNameWithoutExtension(file.FileName); file.SaveAs(Server.MapPath(nameAndLocation)); } return View(MVC.Admin.ActionNames.Documents); } 周期的情况下获取此文件?

2 个答案:

答案 0 :(得分:1)

您可以使用FirstOrDefault扩展名方法:

 string fileName = Request.Files.Cast<HttpPostedFile>().FirstOrDefault();

if (!string.IsNullOrEmpty(fileName))
{
}

或者只是带有索引访问器的三元运算符:

string fileName = Request.Files.Count > 0 ? Request.Files[0] : null;

答案 1 :(得分:1)

获取名为file的第一个元素的索引/键:

var imagem = Request.Files[Request.Files.GetKey(0)];