如何使用 MVC3 读取上传的文件?

发布于 2024-10-19 12:27:10 字数 784 浏览 2 评论 0原文

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();
        }

    }
}

我应该让用户上传文件并从中读取,然后显示一条确认消息,表明该文件已保存。问题是:我怎样才能“获取”该流?有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

允世 2024-10-26 12:27:10

这能解决问题吗?

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

与类似的东西结合使用:

<form action="/MyController/Index" enctype="multipart/form-data" method="post">
    <!-- blah -->
    <input type="file" id="excelFile" name="excelFile" />
    <!-- blah -->
</form>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文