Tuesday, 1 October 2013

handling posted images using asp.net mvc

handling posted images using asp.net mvc

I want to save multiple images at once using asp.net mvc4. At view side I
have 5 browse button and I'm successf. getting those file at the
controller side as parameter at post action
IEnumerable<HttpPostedFileBase> postedPhotos.
I want to know if postedPhotos[0] was sent, if so save it to the db.
iterate trough remaining postedPhotos collection and take further action
only to those which actually contains photo (user can send only one or two
image instead of five)
so I tried with
if(postedPhotos.First() != null)
foreach(var photo in postedPhotos.Where(x=>x.ContentLength>0))
{....}
but this doesn't work since I'm touching postedPhoto null value and I'm
getting exception
Object reference not set to an instance of an object.
ofcourse everything works if I send all five photos but I'm interesting
how to handle this situation.
THanks

No comments:

Post a Comment