c# - Request.Files - get the first File without foreach cycle -
i'm new in web. action
:
[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); }
question: know in request.files can 1 file. exist way file without foreach
cycle?
you can use firstordefault
extension method:
string filename = request.files.firstordefault(); if (!string.isnullorempty(filename)) { }
or ternary operator index accessor:
string filename = request.files.count > 0 ? request.files[0] : null;
Comments
Post a Comment