2015-04-08 168 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 
} 
相关问题