excel部署到aws Elastic beantalk后,excel上传失败

时间:2019-10-24 17:12:33

标签: c# asp.net asp.net-mvc amazon-web-services amazon-elastic-beanstalk

我有一个MVC应用程序,允许管理员用户将excel文件上传到系统上;然后,控制器使用excel数据创建一个数据集,然后使用SqlBulkCopy使用该数据集填充“学校”和“ School2”数据库。

当我使用IIS Express在本地测试这些上传文件时,它们完美工作,尽管当我按下导入按钮时,部署到AWS Elastic beantalk的同一版本也会引发错误。控制台中的错误显示为:无法加载资源:服务器以状态500(内部服务器错误)进行了响应

有人能看到任何清楚的理由来说明为什么此操作在本地可以正常运行,但在部署到弹性beantalk时却无法正常工作吗?

我的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();
        }
    }

欢呼

编辑:我的数据库和安全组设置正确,因为我可以从应用程序上的数据库查看实体框架数据列表。我也可以登录,以便数据库连接良好。唯一的问题是,上载按钮在部署时不起作用(尽管它确实在IIS上本地起作用)

1 个答案:

答案 0 :(得分:0)

检查IAM角色的权限,允许IP从服务器A到服务器B的访问。