delete
delete

Reputation:

How can I read an uploaded file using MVC3?

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?

Upvotes: 1

Views: 2424

Answers (1)

ten5peed
ten5peed

Reputation: 15890

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>

Upvotes: 2

Related Questions