简体   繁体   中英

excel upload fails after deploying to aws elastic beanstalk

I have an MVC application which allows an admin user to upload an excel file onto the system; the controller then creates a dataset with the excel data and then populates a "Schools" & a "School2" database with the dataset using an SqlBulkCopy.

These uploads work perfect when I test them locally using IIS Express, although the same version deployed to aws elastic beanstalk throws an error when I press the import button. The error in the console shows as: Failed to load resource: the server responded with a status of 500 (Internal Server Error)

Could anybody see any clear reasons why this action works fine locally but does not work when deployed to elastic beanstalk?

My UploadController:

    public class UploadController : Controller
    {
        SqlConnection con = new SqlConnection(@"Data Source=bookingdb.cwln7mwjvxdd.eu-west-1.rds.amazonaws.com,1433;Initial Catalog=modeldb;User ID=USER;Password=PASSWORD;Database=modeldb;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");

        OleDbConnection Econ;

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(HttpPostedFileBase file)
        {
            string filename = Guid.NewGuid() + Path.GetExtension(file.FileName);
            string filepath = "/excelfolder/" + filename;
            file.SaveAs(Path.Combine(Server.MapPath("/excelfolder"), filename));
            InsertExceldata(filepath, filename);
            return View();
        }

        [HttpPost]
        public ActionResult Index2(HttpPostedFileBase file)
        {
            string filename = Guid.NewGuid() + Path.GetExtension(file.FileName);
            string filepath = "/excelfolder/" + filename;
            file.SaveAs(Path.Combine(Server.MapPath("/excelfolder"), filename));
            InsertExceldata2(filepath, filename);
            return RedirectToAction("Index");
        }

        private void ExcelConn(string filepath)
        {
            string constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", filepath);
            Econ = new OleDbConnection(constr);
        }

        private void InsertExceldata(string filepath, string filename)
        {
            string fullpath = Server.MapPath("/excelfolder/") + filename;
            ExcelConn(fullpath);
            string query = string.Format("Select * from [{0}]", "Sheet1$");
            OleDbCommand Ecom = new OleDbCommand(query, Econ);
            Econ.Open();

            DataSet ds = new DataSet();
            OleDbDataAdapter oda = new OleDbDataAdapter(query, Econ);
            Econ.Close();
            oda.Fill(ds);

            DataTable dt = ds.Tables[0];

            SqlBulkCopy objbulk = new SqlBulkCopy(con);
            objbulk.DestinationTableName = "dbo.Schools";
            objbulk.ColumnMappings.Add("AcademicYear", "AcademicYear");
            objbulk.ColumnMappings.Add("RollNumber", "RollNumber");
            objbulk.ColumnMappings.Add("OfficialSchoolName", "OfficialSchoolName");
            objbulk.ColumnMappings.Add("Address1", "Address1");
            objbulk.ColumnMappings.Add("Address2", "Address2");
            objbulk.ColumnMappings.Add("Address3", "Address3");
            objbulk.ColumnMappings.Add("Address4", "Address4");
            objbulk.ColumnMappings.Add("County", "County");
            objbulk.ColumnMappings.Add("Eircode", "Eircode");
            objbulk.ColumnMappings.Add("LocalAuthority", "LocalAuthority");
            objbulk.ColumnMappings.Add("X", "X");
            objbulk.ColumnMappings.Add("Y", "Y");
            objbulk.ColumnMappings.Add("ITMEast", "ITMEast");
            objbulk.ColumnMappings.Add("ITMNorth", "ITMNorth");
            objbulk.ColumnMappings.Add("Latitude", "Latitude");
            objbulk.ColumnMappings.Add("Longitude", "Longitude");

            con.Open();
            objbulk.WriteToServer(dt);
            con.Close();
        }

        private void InsertExceldata2(string filepath, string filename)
        {
            string fullpath = Server.MapPath("/excelfolder/") + filename;
            ExcelConn(fullpath);
            string query = string.Format("Select * from [{0}]", "Sheet1$");
            OleDbCommand Ecom = new OleDbCommand(query, Econ);
            Econ.Open();

            DataSet ds = new DataSet();
            OleDbDataAdapter oda = new OleDbDataAdapter(query, Econ);
            Econ.Close();
            oda.Fill(ds);

            DataTable dt = ds.Tables[0];

            SqlBulkCopy objbulk = new SqlBulkCopy(con);
            objbulk.DestinationTableName = "dbo.School2";
            objbulk.ColumnMappings.Add("RollNumber", "RollNumber");
            objbulk.ColumnMappings.Add("OfficialSchoolName", "OfficialSchoolName");
            objbulk.ColumnMappings.Add("Address1", "Address1");
            objbulk.ColumnMappings.Add("Address2", "Address2");
            objbulk.ColumnMappings.Add("Address3", "Address3");
            objbulk.ColumnMappings.Add("Address4", "Address4");
            objbulk.ColumnMappings.Add("County", "County");
            objbulk.ColumnMappings.Add("Eircode", "Eircode");
            objbulk.ColumnMappings.Add("PhoneNumber", "PhoneNumber");
            objbulk.ColumnMappings.Add("Email", "Email");
            objbulk.ColumnMappings.Add("PrincipalName", "PrincipalName");
            objbulk.ColumnMappings.Add("DeisSchool", "DeisSchool");
            objbulk.ColumnMappings.Add("SchoolGender", "SchoolGender");
            objbulk.ColumnMappings.Add("PupilAttendanceType", "PupilAttendanceType");
            objbulk.ColumnMappings.Add("IrishClassification", "IrishClassification");
            objbulk.ColumnMappings.Add("GaeltachtArea", "GaeltachtArea");
            objbulk.ColumnMappings.Add("FeePayingSchool", "FeePayingSchool");
            objbulk.ColumnMappings.Add("Religion", "Religion");
            objbulk.ColumnMappings.Add("OpenClosedStatus", "OpenClosedStatus");
            objbulk.ColumnMappings.Add("TotalGirls", "TotalGirls");
            objbulk.ColumnMappings.Add("TotalBoys", "TotalBoys");
            objbulk.ColumnMappings.Add("TotalPupils", "TotalPupils");

            con.Open();
            objbulk.WriteToServer(dt);
            con.Close();
        }
    }

Cheers

edit: My database and security groups are set up correctly as I can view lists of entity framework data from the database on my application. I can also login also so the databases are connected fine. The only problem is the upload button does not work when deployed (it does work locally on IIS though)

Check the permissions on the IAM role, Allow the IP access from server A to server B.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM