【问题标题】:How can I read an uploaded file using MVC3?如何使用 MVC3 读取上传的文件?
【发布时间】:2011-02-24 21:46:40
【问题描述】:
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();
        }

    }
}

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

【问题讨论】:

  • 这篇文章可能会有所帮助:stackoverflow.com/questions/1653469/…
  • @Ardman:我看不到他实际上传文件的位置。 [发布]操作在哪里?
  • @Sergio:看看cottsak的第三个代码块。
  • @Ardman:你是这个意思吗? FileManagerController.FileUploadResultDTO files = FileManagerController.GetFilesFromRequest((HttpContextWrapper)HttpContext); 我认为这是他制作的自定义类,但没有共享代码。是内置的吗?
  • @Sergio:您阅读完整答案了吗?他已经分享了代码。

标签: file-upload asp.net-mvc-3 stream


【解决方案1】:

这样可以解决问题吗?

[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>

【讨论】:

  • excelFile是什么对象类型?那是自定义对象类型吗?
  • 没关系,我想你把参数的顺序搞混了。我会尝试修复它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-21
  • 2014-10-10
  • 2012-07-30
相关资源
最近更新 更多