保存上传到azurewebsite上的文件

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

发布我的MVC4 web项目后,上传的Excel文件有问题。 我加载一个HttpPostedFileBase并将path发送给我的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 …第二也许有一种方法来保存文件。

我很想得到解决这个问题的build议…

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

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

最后的结果是:

 [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是avalibale 兔子我添加Excel.dll的引用,我添加使用Excel;

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

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

检查你在Session中储存的exception – 你应该在这里find更多的信息。

@hoonzis是正确的,写入文件到云中的磁盘是不允许的(你不能事件获取或设置文件的path)。 你应该使用blob存储,更有效的文件和比sql便宜。 我build议使用表存储服务,它是noSQL,但它比天青SQL更便宜。 只有在您的解决scheme必须使用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/

有关select正确存储的更多信息,请点击此处: http : //www.windowsazure.com/en-us/develop/net/fundamentals/cloud-storage-scenarios/