Note that there are some explanatory texts on larger screens.

plurals
  1. POSave file uploaded on azurewebsite
    primarykey
    data
    text
    <p>On my local machine everything works well but..</p> <p>After publishing my <code>MVC4</code> web project there is a problem with an uploaded Excel file. I load an <code>HttpPostedFileBase</code> and send the path to my BL. There I load it to <code>dataTable</code> and on my second call I get it to a <code>list</code>.</p> <p>Here is the code..</p> <p>Controller:</p> <pre><code> [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 =&gt; x.SerialNumber == item.SerialNumber)) cardsToUpdate.Remove(item); } Session["InfoMsg"] = "click update to finish"; } catch (Exception ex) { Session["ErrorMsg"] = ex.Message; } return View("viewUploadCards", cardsToUpdate); } </code></pre> <p>BL:</p> <pre><code> 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; } </code></pre> <p>and:</p> <pre><code> public List&lt;Card&gt; getUpdateCards(DataTable dt, int prodId) { List&lt;Card&gt; cards = new List&lt;Card&gt;(); try { Product product = db.Products.Single(p =&gt; 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; } </code></pre> <p>Now I think <code>Windows Azure</code> doesn't let me save this file because on the middle view when I supposed to see the data - I don't see it.</p> <p>I thought of some ways... one - not saving the file, but I don't see how to complete the <code>ConnectionString</code>... second maybe there is a way to save the file there.</p> <p>I'd love to get suggestions for solving this problem...</p> <p>10x and sorry for my bad English =)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload