gpt4 book ai didi

c# - 保存上传到 azure 网站的文件

转载 作者:行者123 更新时间:2023-11-30 17:07:28 25 4
gpt4 key购买 nike

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

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

这是代码..

Controller :

  [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 不允许我保存此文件,因为在中间 View 中,当我应该看到数据时 - 我没有看到它。

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

我很想获得解决此问题的建议...

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

最佳答案

我很尴尬,但我发现了类似的问题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 的引用,并添加了 using Excel;

关于c# - 保存上传到 azure 网站的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14507514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com