Excel文件上载在本地工作但不在Azure上工作

时间:2018-01-12 21:10:29

标签: asp.net azure

在Azure上使用ASP.net MVC。作为主题说明,它在本地运行良好,但在Azure上运行不正常。表单正确呈现。提交表单后,我会收到页面标题和一条消息,说明"处理您的请求时出错。"我已检查过数据库,所有字段都已正确转移。我不确定还有什么用于排除故障。

namespace WebApplication12.Controllers
{
[Authorize]
public class DashboardController : Controller
{

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
    OleDbConnection Econ;

    // GET: Dashboard
    public ActionResult Index()
    {
        return View();
    }
    [HttpPost]
    public ActionResult Index(HttpPostedFileBase file)
    {
        string filename = Guid.NewGuid() + Path.GetDirectoryName(file.FileName);
        string filepath = "/excel/" + filename;
        file.SaveAs(Path.Combine(Server.MapPath("/excel/"), filename));
        InsertExcelData(filepath, filename);
        ViewBag.Message = "File successfully uploaded.";
        return View();
    }

    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;IMEX=1\";", filepath);
        Econ = new OleDbConnection(constr);
    }

    private void InsertExcelData(string filepath, string filename)
    {
        string fullpath = Server.MapPath("/excel/") + 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];
        dt.Columns.Add(new DataColumn("Email", typeof(System.String)));
        dt.Columns.Add(new DataColumn("TimeStamp", typeof(System.DateTime)));

        foreach (DataRow dr in dt.Rows)
        {
            dr["Email"] = User.Identity.GetUserId(); 
            dr["TimeStamp"] = DateTime.Now.ToString();
        }

        SqlBulkCopy objbulk = new SqlBulkCopy(con);
        objbulk.DestinationTableName = "Upload";
        objbulk.ColumnMappings.Add("Email", "Email");
        objbulk.ColumnMappings.Add("TimeStamp", "TimeStamp");
        objbulk.ColumnMappings.Add("EmployeeId", "EmployeeId");
        objbulk.ColumnMappings.Add("Name", "Name");
        objbulk.ColumnMappings.Add("Title", "Title");
        objbulk.ColumnMappings.Add("Department", "Department");
        objbulk.ColumnMappings.Add("Race", "Race");
        objbulk.ColumnMappings.Add("Gender", "Gender");
        objbulk.ColumnMappings.Add("AnnualizedBase", "AnnualizedBase");
        objbulk.ColumnMappings.Add("AnnualizedTCC", "AnnualizedTCC");
        con.Open();
        objbulk.WriteToServer(dt);
        con.Close();


    }
}  
} 

谢谢!

1 个答案:

答案 0 :(得分:0)

在云环境中,您通常不会写入Web服务器文件系统。相反,您将上传保存到blob存储,并记录保存它的路径以及持久数据记录。

Here is an article作者使用Storage API将上传的图像发送到blob。但这是使用blob存储解决的一个相当标准的挑战,因此the Microsoft documentation可能会为您提供足够的信息以取得进展。结合了MVC和blob存储的Here is an official Microsoft sample MVC controller