2015-04-08 200 views
-2

通過以下代碼,我可以將文件保存到文件夾。將文件上傳到文件夾

我的問題是隻有兩個上傳字段是強制性的,其餘三個不是。如果所有上傳字段都有一個選中的文件,則其代碼將起作用NullReferenceException

if (AnnualReport != null || ProjectReports != null || Publications != null || Other != null || RegistDoc != null) { 
    int filesize = AnnualReport.PostedFile.ContentLength; 
    int filesizeP = ProjectReports.PostedFile.ContentLength; 
    int filesizePub = Publications.PostedFile.ContentLength; 
    int filesizeOther = Other.PostedFile.ContentLength; 
    int filesizeReg = RegistDoc.PostedFile.ContentLength; 
    if (filesize > 2097152 && filesizeP > 2097152 && filesizePub > 1048576 && filesizeOther > 1048576 && filesizeReg > 1048576) { 
    ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Maximum File size For Annual/Project reports is 1.5MB and for the Publications/Other Attachemnets is 1MB');", true); 
    } else { 
    const string ReportDirectory = "REPORTS/"; 
    //Other Document 
    string OtherPath = ReportDirectory + Other.FileName; 
    string fileNameWithoutExtensionOther = System.IO.Path.GetFileNameWithoutExtension(Other.FileName); 
    int iterationOther = 1; 
    while (System.IO.File.Exists(Server.MapPath(OtherPath))) { 
     OtherPath = string.Concat(ReportDirectory, fileNameWithoutExtensionOther, "-", iterationOther, ".pdf"); 
     iterationOther++; 
    } 
    //Registration Document 
    string RigisDocPath = ReportDirectory + RegistDoc.FileName; 
    string fileNameWithoutExtensionRegis = System.IO.Path.GetFileNameWithoutExtension(RegistDoc.FileName); 
    int iterationRE = 1; 
    while (System.IO.File.Exists(Server.MapPath(RigisDocPath))) { 
     RigisDocPath = string.Concat(ReportDirectory, fileNameWithoutExtensionRegis, "-", iterationRE, ".pdf"); 
     iterationRE++; 
    } 
    //Annual Reports 
    string ReportPath = ReportDirectory + AnnualReport.FileName; 
    string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(AnnualReport.FileName); 
    int iteration = 1; 
    while (System.IO.File.Exists(Server.MapPath(ReportPath))) { 
     ReportPath = string.Concat(ReportDirectory, fileNameWithoutExtension, "-", iteration, ".pdf"); 
     iteration++; 
    } 
    //Project Report 
    string ProjecttPath = ReportDirectory + ProjectReports.FileName; 
    string fileNameWithoutExtensionP = System.IO.Path.GetFileNameWithoutExtension(ProjectReports.FileName); 
    int iterationP = 1; 
    while (System.IO.File.Exists(Server.MapPath(ProjecttPath))) { 
     ProjecttPath = string.Concat(ReportDirectory, fileNameWithoutExtensionP, "-", iterationP, ".pdf"); 
     iterationP++; 
    } 
    //publication 
    string publicationPath = ReportDirectory + Publications.FileName; 
    string fileNameWithoutExtensionPub = System.IO.Path.GetFileNameWithoutExtension(Publications.FileName); 
    int iterationPub = 1; 
    while (System.IO.File.Exists(Server.MapPath(publicationPath))) { 
     publicationPath = string.Concat(ReportDirectory, fileNameWithoutExtensionPub, "-", iterationPub, ".pdf"); 
     iterationPub++; 
    } 
    ProjectReports.SaveAs(Server.MapPath(ProjecttPath)); 
    AnnualReport.SaveAs(Server.MapPath(ReportPath)); 
    Publications.SaveAs(Server.MapPath(publicationPath)); 
    RegistDoc.SaveAs(Server.MapPath(RigisDocPath)); 
    Other.SaveAs(Server.MapPath(OtherPath)); 
+1

你可能是個初學者,但你還是應該格式化代碼在你的文章中,所以它很容易閱讀。你也應該告訴我們你拋出異常的地方,而不是讓我們猜測。 – mason

+0

可能重複[什麼是NullReferenceException,我該如何解決它?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – mason

+0

這不是一個答案,但更多的是一種不同方法的建議。對於每個要檢查null的對象,都可以創建一個接口或抽象類,該類不包含這些類對象的實現。這樣,您可以傳遞實現所述接口的基礎對象或對象的實例,並檢查它是否爲空。如果不是,請確定它的類型並正確處理。這將有助於減少代碼中的一些混亂,並使其更易於閱讀和解釋。如果你需要一個例子,我很樂意把它們放在一起。 –

回答

0

我沒有將代碼燒入像@irreal建議的不同方法,如下所示;

public void PublicationReporting() { 

      //connection for the datareader 
      string csoWConn = ConfigurationManager.ConnectionStrings["RegisterCon"].ToString(); 
      SqlConnection csoW_connection = new SqlConnection(csoWConn); 
      string database = csoW_connection.DataSource.ToString(); 
      csoW_connection.Open(); 

      if (Publications == null) 
      { 
       Publications.Dispose(); 

       //// 
       String MyString = @"UPDATE tb_Quadrennial_Report SET PublicationsPath='' WHERE Org_ID = '" + Accrediated_Orgs.SelectedValue + "'"; 
       SqlCommand MyCmd = new SqlCommand(MyString, csoW_connection); 
       int LastInsertedRecordID; 

       LastInsertedRecordID = Convert.ToInt32(MyCmd.ExecuteScalar()); 


      } 
      else 
      { 
       int filesizeP = Publications.PostedFile.ContentLength; 

       if (filesizeP > 2097152) 
       { 
        ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Maximum File size For Publication is 2.0 MB');", true); 
       } 

       else 
       { 

        const string ReportDirectory = "REPORTS/"; 

        //publication 
        string publicationPath = ReportDirectory + Publications.FileName; 
        string fileNameWithoutExtensionPub = System.IO.Path.GetFileNameWithoutExtension(Publications.FileName); 

        int iteration = 1; while (System.IO.File.Exists(Server.MapPath(publicationPath))) 
        { 
         publicationPath = string.Concat(ReportDirectory, fileNameWithoutExtensionPub, "-", iteration, ".pdf"); 
         iteration++; 
        } 


        Publications.SaveAs(Server.MapPath(publicationPath)); 

        String MyString = @"UPDATE tb_Quadrennial_Report SET PublicationsPath='" + publicationPath + "' WHERE Org_ID = '" + Accrediated_Orgs.SelectedValue + "'"; 
        SqlCommand MyCmd = new SqlCommand(MyString, csoW_connection); 
        int LastInsertedRecordID; 

        LastInsertedRecordID = Convert.ToInt32(MyCmd.ExecuteScalar()); 
       } 

      } 

     } 

我當時叫IT運click事件

try{ 
           PublicationReporting(); 
         } 

          catch (Exception ex) 
         { 

          pgError.Text = "Publication Exception Message: " + ex.Message; 

         } 

          finally 
          { 
           csoW_connection.Close(); 
          } 

從這裏,這是很容易弄清楚的問題。

我只是需要處理的上傳字段內容,如果沒有文件被選中這樣

public void PublicationReporting() { 

      //connection for the datareader 
      string csoWConn = ConfigurationManager.ConnectionStrings["RegisterCon"].ToString(); 
      SqlConnection csoW_connection = new SqlConnection(csoWConn); 
      string database = csoW_connection.DataSource.ToString(); 
      csoW_connection.Open(); 

      if (Publications == null) 
      { 
       Publications.Dispose(); 

       //// 
       String MyString = @"UPDATE tb_Quadrennial_Report SET PublicationsPath='' WHERE Org_ID = '" + Accrediated_Orgs.SelectedValue + "'"; 
       SqlCommand MyCmd = new SqlCommand(MyString, csoW_connection); 
       int LastInsertedRecordID; 

       LastInsertedRecordID = Convert.ToInt32(MyCmd.ExecuteScalar()); 


      } 
      else{ 

//程序繼續}

0

您發佈的代碼格式很差。但是,解決您的直接問題是將空檢查移至每個單獨的文檔。

而不是做一個巨大的,如果線(其中有疑問的邏輯,因爲它只是檢查是否有任何的文件被上傳)

你可以只檢查是否需要文件存在的。 (查看你的代碼,目前意味着文檔名稱對象不爲空) 如果沒有,則拋出一個錯誤。

如果是,則繼續執行其餘代碼,但將可選文檔的各個處理包裝在自己的空檢查if-s中。

即。

if (AnnualReport != null) { 
//the block that does stuff with the anual report object 
} 
相關問題