在我的本地机器上一切正常,但是..
发布我的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);
}
提单:
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 倍,对不起我的英语不好 =)