【问题标题】:Excel File Upload Working Locally But Not On AzureExcel 文件上传在本地工作,但不在 Azure 上
【发布时间】:2018-01-13 11:40:09
【问题描述】:

在 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();


    }
}  
} 

谢谢!

【问题讨论】:

    标签: asp.net azure


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-03
      • 1970-01-01
      • 2021-09-15
      • 1970-01-01
      • 2013-04-23
      • 2016-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多