2012-08-07 2 views
1

몇 개의 사진이있는 거리가 있습니다. 새로운 거리 개체를 만드는 위치와 동일한 형식으로 업로드를 구현하려고합니다. 그래서 이미지 업로드 및 image.ContentLenght> 0

이제 필드

  • StreetName
  • StreetNumber
  • 사진 1
  • '사진
  • 사진 2와 이미지 MVC 웹 양식의 create.cshtml 페이지에 나는 StreetName과 양식을 가지고 및 StreetNumber 필드 및

    나는 웹 양식의 모든 것을에서 제공되는 이미지의 정확한 숫자를 게시 할 경우 내 주석 라인 내에서 읽을 수 있듯이 더 공정

    [HttpPost] 
    public ActionResult Create(StreetViewModel newData, IEnumerable<HttpPostedFileBase> postedImages) 
    { 
        //create object and save stretname and streetnumber 
        //process posted images 
        foreach(var image in postedImages) 
        { 
         //this is where error occured if I post only one or two images from my view 
         if(image.ContentLength >0) 
         { 
          //process and save images 
         } 
        } 
    
    } 
    

    의 StreetController에이 데이터를 전송하고있어이 양식을 제출 중에 23,

    괜찮아요,하지만 1 또는 2 이미지를 보내면 오류가 발생했습니다.

    해결 방법? 덕분에

답변

2

ContentLength proeprty

if((image!=null) && (image.ContentLength >0)) 
{ 
     //process and save images 
} 
2

에 접근하기 전에 null 검사를 추가 foreach(var image in postedImages.Where(pi => pi != null))

2

당신은 이런 일을 할 수있는 :

If(image != null) 
    if(image.ContentLength > 0) 
     { 
      //code 
     } 

확인하는 경우는 속성을 얻고있다 전에 널 (null) 이것이 오류의 원인입니다.