【问题标题】:Save file uploaded on azurewebsite保存上传到 azurewebsite 的文件
【发布时间】:2013-01-24 17:47:14
【问题描述】:

在我的本地机器上一切正常,但是..

发布我的MVC4 Web 项目后,上传的 Excel 文件出现问题。 我加载了HttpPostedFileBase 并将路径发送到我的 BL。在那里,我将其加载到 dataTable,然后在第二次调用时,我将其加载到 list

这是代码..

控制器:

  [HttpPost]
    public ActionResult UploadCards(HttpPostedFileBase file, string sheetName, int ProductID)
    {
        try
        {
            if (file == null || file.ContentLength == 0)
                throw new Exception("The user not selected a file..");

            var fileName = Path.GetFileName(file.FileName);
            var path = Server.MapPath("/bin");

            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            path = Path.Combine(path, fileName);
            file.SaveAs(path);

            DataTable cardsDataTable = logic.LoadXLS(path, sheetName);
            cardsToUpdate = logic.getUpdateCards(cardsDataTable, ProductID);

            foreach (var item in cardsToUpdate)
            {
                if (db.Cards.ToList().Exists(x => x.SerialNumber == item.SerialNumber))
                    cardsToUpdate.Remove(item);
            }
            Session["InfoMsg"] = "click update to finish";
        }
        catch (Exception ex)
        {
            Session["ErrorMsg"] = ex.Message;
        }
        return View("viewUploadCards", cardsToUpdate);
    }

BL:

     public DataTable LoadXLS(string strFile, String sheetName)
    {
        DataTable dtXLS = new DataTable(sheetName);

        try
        {
            string strConnectionString = "";

            if (strFile.Trim().EndsWith(".xlsx"))
                strConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", strFile);
            else if (strFile.Trim().EndsWith(".xls"))
                strConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";", strFile);

            OleDbConnection SQLConn = new OleDbConnection(strConnectionString);

            SQLConn.Open();

            OleDbDataAdapter SQLAdapter = new OleDbDataAdapter();

            string sql = "SELECT * FROM [" + sheetName + "$]";

            OleDbCommand selectCMD = new OleDbCommand(sql, SQLConn);

            SQLAdapter.SelectCommand = selectCMD;

            SQLAdapter.Fill(dtXLS);

            SQLConn.Close();

        }

        catch (Exception ex)
        {
            string res = ex.Message;
            return null;
        }

        return dtXLS;
    }

和:

    public List<Card> getUpdateCards(DataTable dt, int prodId)
    {
        List<Card> cards = new List<Card>();
        try
        {
            Product product = db.Products.Single(p => p.ProductID == prodId);
            foreach (DataRow row in dt.Rows)
            {
                cards.Add(new Card
                {
                    SerialNumber = row[0].ToString(),
                    UserName = row[1].ToString(),
                    Password = row[2].ToString(),

                    Activated = false,

                    Month = product.Months,
                    Bandwidth = product.Bandwidth,
                    ProductID = product.ProductID,
                    // Product = product
                });
            }
        }
        catch (Exception ex)
        {
            db.Log.Add(new Log { LogDate = DateTime.Now, LogMsg = "Error : " + ex.Message });

        }
        return cards;
    }

现在我认为Windows Azure 不允许我保存此文件,因为在我应该看到数据的中间视图上 - 我没有看到它。

我想到了一些方法... 一 - 不保存文件,但我看不到如何完成ConnectionString... 其次,也许有一种方法可以将文件保存在那里。

我很想得到解决这个问题的建议......

10 倍,抱歉我的英语不好 =)

【问题讨论】:

标签: c# excel file azure asp.net-mvc-4


【解决方案1】:

我很尴尬,但我发现了一个类似的问题here.. 不完全是,但它给了我一个很好的方向。

最后的结果:

[HttpPost]
    public ActionResult UploadCards(HttpPostedFileBase file, string sheetName, int ProductID)
    {
        IExcelDataReader excelReader = null;
        try
        {
            if (file == null || file.ContentLength == 0)
                throw new Exception("The user not selected a file..");

            if (file.FileName.Trim().EndsWith(".xlsx"))
                excelReader = ExcelReaderFactory.CreateOpenXmlReader(file.InputStream);
            else if (file.FileName.Trim().EndsWith(".xls"))
                excelReader = ExcelReaderFactory.CreateBinaryReader(file.InputStream);
            else
                throw new Exception("Not a excel file");

            cardsToUpdate = logic.getUpdateCards(excelReader.AsDataSet().Tables[sheetName], ProductID);

            foreach (var item in cardsToUpdate)
            {
                if (db.Cards.ToList().Exists(x => x.SerialNumber == item.SerialNumber))
                    cardsToUpdate.Remove(item);
            }
            Session["InfoMsg"] = "Click Update to finish";
        }
        catch (Exception ex)
        {
            Session["ErrorMsg"] = ex.Message;
        }
        finally
        {
            excelReader.Close();
        }
        return View("viewUploadCards", cardsToUpdate);
    }  

10q 全部。

编辑:下载、参考和使用

dll 可用hare 我添加对 Excel.dll 的引用并添加使用 Excel;

【讨论】:

    【解决方案2】:

    问题可能是由于将文件写入磁盘造成的。云提供商通常不允许应用程序写入磁盘。

    在您的情况下,文件似乎只是临时写入磁盘并直接加载到数据库中。您应该能够直接从上传的文件中打开流并将其直接写入数据库 - 无需写入磁盘。

    检查您在 Session 中库存的异常 - 您应该在那里找到更多信息。

    【讨论】:

      【解决方案3】:

      @hoonzis 是对的,不允许将文件写入云中的磁盘(您不能事件获取或设置文件的路径)。您应该使用 blob 存储,它对文件的效率更高,并且比 sql 更便宜。我推荐使用表存储服务,它是 noSQL 但它比 azure sql 便宜。 仅当您的解决方案必须使用 azure sql。

      Blob 存储在此处查看更多详细信息:http://www.windowsazure.com/en-us/develop/net/how-to-guides/blob-storage/

      表存储:http://www.windowsazure.com/en-us/develop/net/how-to-guides/table-services/

      您可以在此处找到有关选择正确存储的更多信息:http://www.windowsazure.com/en-us/develop/net/fundamentals/cloud-storage-scenarios/

      【讨论】:

      • 无论如何我们都使用 sql.. 该服务仅用于上传一种产品.. 我不需要只将数据保留到我的 dbContex。有更好的方法吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-02
      • 2012-12-15
      • 1970-01-01
      • 2017-12-14
      • 2017-04-08
      相关资源
      最近更新 更多