2009-12-21 5 views
0

사용자가 고객 번호를 입력 한 다음 업로드 할 파일을 선택한 다음 설명을 입력하고 저장 버튼을 누릅니다. 그러나 파일을 선택했는지 확인하기 위해 항상 false를 반환합니다. 이유를 알고 싶습니다.내 File.HasFile이 항상 false를 반환하는 이유

편집 : 이제 enctype = "multipart/form-data"매개 변수가있는 양식을 사용 중이며 업데이트 패널에 래핑됩니다.

else if ((images.Selected == true || docs.Selected == true) && 
        (Upload.HasFile == false || txtInfo.Text == string.Empty)) 
     { 
      if (Upload.HasFile == false) 
       lblErrorMessage.Text = "You haven't selected a file"; 
      else lblErrorMessage.Text = "You must enter a description"; 

      validated = false; 
     } 

그리고 여기, 저장 버튼에서 온 방법이다 : 양식 및 업데이트 패널 모두 여기에 해당 오류를 처리하는 코드의 조각이야와 HasFile() 값을 받고 마스터 페이지

protected void btnSaveNew_Click(object sender, EventArgs e) 
    { 
     bool validated = true; 

     lblErrorMessage.ForeColor = System.Drawing.Color.Red; 

     if (txtCustomerNumber.Text.Length != 8) 
     { 
      if (txtCustomerNumber.Text.Length == 0) 
       lblErrorMessage.Text = "Hey! What about the customer number?"; 
      else if (txtCustomerNumber.Text.Length > 8) 
       lblErrorMessage.Text = "Invalid Customer Number length (" + (txtCustomerNumber.Text.Length - 8) + " char(s) too long)"; 
      else lblErrorMessage.Text = "Invalid Customer Number length (" + (8 - txtCustomerNumber.Text.Length) + " char(s) short)"; 

      validated = false; 
     } 

     else if (links.Selected == true && (txtLink.Text == string.Empty || txtInfo.Text == string.Empty)) 
     { 
      if (txtLink.Text == string.Empty) 
       lblErrorMessage.Text = "Hey! You forgot to enter a link!"; 
      else lblErrorMessage.Text = "You must enter a description"; 

      validated = false; 
     } 

     else if ((images.Selected == true || docs.Selected == true) && 
        (Upload.HasFile == false || txtInfo.Text == string.Empty)) 
     { 
      if (Upload.HasFile == false) 
       lblErrorMessage.Text = "You haven't selected a file"; 
      else lblErrorMessage.Text = "You must enter a description"; 

      validated = false; 
     } 

     else if (txtInfoDesc.Text == string.Empty || txtInfo.Text == string.Empty) 
     { 
      if (txtInfoDesc.Text == string.Empty) 
       lblErrorMessage.Text = "You must enter an info description"; 
      else lblErrorMessage.Text = "You must enter info Data"; 

      validated = false; 
     } 

     if (validated == false) 
     { 
      btnModify.Visible   = false; 
      btnCreateNew.Visible  = false; 
      ddlCustomerNumber.Visible = false; 
      btnSaveNew.Visible   = true; 
      btnCancel.Visible   = true; 
      txtCustomerNumber.Visible = true; 

      switch (rblSection.SelectedIndex) 
      { 
       case 0: txtInfo.Visible = true; 
         txtLink.Visible = true; 
         break; 
       case 1: 
       case 2: txtInfo.Visible = true; 
         Upload.Visible = true; 
         break; 
       case 3: txtInfo.Visible  = true; 
         txtInfoDesc.Visible = true; 
         break; 
      } 

      if (GetCategoryIDCookie() != 0) 
       divData.Attributes.Add("Style", "overflow:auto"); 
     } 
     else 
     { 
      addNewCustomerNumber(txtCustomerNumber.Text, txtInfoDesc.Text, txtInfo.Text); 
      ddlCustomerNumber.Visible = true; 
      divData.Attributes.Remove("Style"); 
      Response.Redirect(Request.RawUrl); 
     } 
    } 
+0

우연히 업데이트 패널 내부에 있습니까? –

+1

양식 태그에 enctype = "multipart/form-data"가 있습니까? –

+0

@ 미첼 - 그래. – Justen

답변

2

파일 업로드를 둘러싼 업데이트 패널이 있습니까?

+0

예. 또한,이 updatePanel은 masterfile에 있으며 PostBackTriggers를 할 필요가 있다는 것을 배웠습니다. 그러나 master 파일에서이를 수행하는 방법을 설명하는 기사를 찾을 수 없습니다. 방아쇠. – Justen

+0

업데이트 패널을 삭제하고 결국 문제가 해결되었습니다. 이 코드는 이전의 누군가에 의해 수행되었으며 기능은 완전히 동일합니다. – Justen

1

양식 태그에 enctype = "multipart/form-data"가 있습니까?

+0

나는 그것을하지 않았지만 이것을 추가하는 것은 도움이되지 못했다. – Justen

0

업로드 클래스의 코드를 게시 할 수 있습니까? 어쩌면 틀린 것으로 그 변수를 잘못 초기화했을 수도 있고, 어떤 이벤트 동안 그것을 사실로 설정하는 것을 잊어 버렸을 수도 있습니다.

+0

나는 이해하고 있는지 잘 모르겠다. DB에 업로드하기 전에 파일이 있는지 확인하려고합니다. Upload.HasFile과 같은 내 코드의 업로드 이름이 내 ASP의 ID를 참조합니다 : FileUpload 객체 – Justen

관련 문제