1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Excel;
using System.Data;

namespace QuimizaReportes.Controllers
{
    public class UploadController : Controller
    {
        public ActionResult Index()
        {
            //stream is supposed to be the excel file object.
            IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
            excelReader.IsFirstRowAsColumnNames = true;

            DataSet result = excelReader.AsDataSet();

            while (excelReader.Read())
            {

            }

            excelReader.Close();

            return View();
        }

    }
}

I'm supposed to let users upload the file and read from it, then display a confirmation message that it has been saved. The question is: How can I 'get' that stream? Any suggestions?

  • This post might be helpful: http://stackoverflow.com/questions/1653469/how-can-i-upload-a-file-and-save-it-to-a-stream-for-further-preview-using-c – Neil Knight Feb 24 '11 at 21:50
  • @Ardman: I don't see where he actually uploads the file. Where is the [Post]Action? –  Feb 24 '11 at 21:51
  • @Sergio: Look at cottsak's third code block. – Neil Knight Feb 24 '11 at 21:52
  • @Ardman: Do you mean this? **FileManagerController.FileUploadResultDTO files = FileManagerController.GetFilesFromRequest((HttpContextWrapper)HttpContext);** I thought that was a custom class he made but didn't share the code for. Is that built in? –  Feb 24 '11 at 21:56
  • @Sergio: Did you read the full answer? He has shared the code. – Neil Knight Feb 24 '11 at 21:58

1 Answers1

2

Would this do the trick?

[HttpPost]
public ActionResult Index(HttpPostedFileBase excelFile)
{
    IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(excelFile.InputStream);
    //Blah
}

In conjunction with something like:

<form action="/MyController/Index" enctype="multipart/form-data" method="post">
    <!-- blah -->
    <input type="file" id="excelFile" name="excelFile" />
    <!-- blah -->
</form>
Neil Knight
  • 47,437
  • 25
  • 129
  • 188
Charlino
  • 15,802
  • 3
  • 58
  • 74
  • What object type is excelFile? Is that a custom object type? –  Feb 24 '11 at 23:23
  • Nevermind, I think you got the order of the parameters mixed up. I'll try it out with the fix. –  Feb 24 '11 at 23:29