0

I have a input of type file that is sending a HttpPostedFileBase (Excel.xlxs) to my controller. This file do I need to save temporary to use in another action. This HttpPostedFileBase am I converting to DataSet using Excel Data Reader. This works fine when I work directly with the file, but not when I have stored it in TempData.

First action:

[HttpPost]
public ActionResult CompareExcel(ExcelModel model, string submitValue, string compareOption)
{

        TempData["firstFile"] = model.Files[0];
        TempData["secondFile"] = model.Files[1];

        if (submitValue == "Compare calculations")
        {
            var firstFile = TempData["firstFile"];
            var secondFile = TempData["secondFile"];
            if (firstFile == null || secondFile == null)
                return View("CompareExcel");
            else
                return CompareColumn(compareOption);
        }
        //Assume submitValue is "Compare calculations"
}

Second action:

    [HttpPost]
    public ActionResult CompareColumn(string compareOption)
    {
        List<Calculation> cList = new List<Calculation>();
        Calculation calc = new Calculation();
        BigModel m = new BigModel();
        HttpPostedFileBase firstFile = (HttpPostedFileBase)TempData["firstFile"];
        HttpPostedFileBase secondFile = (HttpPostedFileBase)TempData["secondFile"];
        DataSet firstSet = ExcelToDataSet.ConvertToDataSet(firstFile);
        DataSet secondSet = ExcelToDataSet.ConvertToDataSet(secondFile);
        for (var i = 0; i < firstSet.Tables[0].Rows.Count; i++) //Get Object reference not set to an instance of an object
        {
            DataRow data = firstSet.Tables[0].Rows[i];
            calc.PresentValue = Convert.ToDecimal(data.Table.Rows[i]["Capital balance"]);
        }

        return View();
    }

My model:

public partial class GetExcel
{

    public List<HttpPostedFileBase> Files { get; set; }

    public GetExcel()
    {
        Files = new List<HttpPostedFileBase>();
    }
}

My method to convert my HttpPostedFileBase (Excel.xlxs) to DataSet. The method is working when sending the file directly but not when using TempData:

public static DataSet ConvertToDataSet(HttpPostedFileBase excelFile)
    {
        DataSet result = null;

        if (excelFile != null && excelFile.ContentLength > 0)
        {
            // .xlsx
            IExcelDataReader reader = ExcelReaderFactory.CreateOpenXmlReader(excelFile.InputStream);

            // .xls
            //IExcelDataReader reader = ExcelReaderFactory.CreateBinaryReader(file.InputStream);

            reader.IsFirstRowAsColumnNames = true; // if your first row contains column names
            result = reader.AsDataSet();
            reader.Close();
        }
        return result;
    }

When I debug and look at firstFile and secondFile I can see there is something there. I can see the filename of the HttpPostedFileBase but when I use ConvertToDataSet it fails in the first if. So obviously it's something wrong with the file saved in TempData. Is it possible to make this work with TempData or can someone suggest some easy way to go around this? Should it work and I'm doing something wrong? The whole thing is that the HttpPostedFileBase somehow needs to be saved temporary (in a easy way).

MrProgram
  • 5,044
  • 13
  • 58
  • 98

0 Answers0