C# 从MVC中的HttpPostedFileBase获取值

C# 从MVC中的HttpPostedFileBase获取值,c#,asp.net,asp.net-mvc,html,razor,C#,Asp.net,Asp.net Mvc,Html,Razor,我的控制器: [HttpPost] public ActionResult ShowExcelFile(HttpPostedFileBase getFile) { //Make something with values of getFile return PartialView("ShowExcelFile"); } 我认为: @using (Html.BeginForm("ShowExcelFile", "ShowExcel"

我的控制器:

    [HttpPost]
    public ActionResult ShowExcelFile(HttpPostedFileBase getFile)
    {
        //Make something with values of getFile
        return PartialView("ShowExcelFile");
    }
我认为:

@using (Html.BeginForm("ShowExcelFile", "ShowExcel", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
     <input type="file" id="getFile" name="getFile" /><br />
     <input type="submit" value="Upload file" />
}
@使用(Html.BeginForm(“showcelfile”、“showcelf”、FormMethod.Post、new{enctype=“multipart/formdata”}))
{

}

如何从getFile读取值?

您可以使用ViewModel将其添加为属性来执行此操作:

查看

@using (Html.BeginForm("ShowExcelFile", "ShowExcel", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
     @Html.TextBoxFor(m => m.Files, new { type = "file", name = "Files" })<br />
     <input type="submit" value="Upload file" />
}

可以使用ViewModel将其添加为属性来执行此操作:

查看

@using (Html.BeginForm("ShowExcelFile", "ShowExcel", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
     @Html.TextBoxFor(m => m.Files, new { type = "file", name = "Files" })<br />
     <input type="submit" value="Upload file" />
}

我不知道你说的是什么意思

用getFile的值制作一些东西

要获取文件的扩展名,请使用

string fileExtension = Path.GetExtension(getFile.FileName);

//save this file using



string path = Path.Combine(Server.MapPath(Url.Content("~/Content")), "Name"+fileExtension);
file.SaveAs(path);

我不知道你说的是什么意思

用getFile的值制作一些东西

要获取文件的扩展名,请使用

string fileExtension = Path.GetExtension(getFile.FileName);

//save this file using



string path = Path.Combine(Server.MapPath(Url.Content("~/Content")), "Name"+fileExtension);
file.SaveAs(path);

解析Excel文件的一种简单方法是使用库,例如(也可作为提供)。安装后,读取Excel文件非常简单

using Excel;

[HttpPost]
public ActionResult ShowExcelFile(HttpPostedFileBase getFile)
{
    if (getFile != null && getFile.ContentLength > 0)
    {
        // .xlsx
        IExcelDataReader reader = ExcelReaderFactory.CreateOpenXmlReader(getFile.InputStream);

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

        reader.IsFirstRowAsColumnNames = true; // if your first row contains column names
    }

    return PartialView("ShowExcelFile");
}
从这一点上讲,如果不知道Excel文件的内容,就很难说出您的确切需求。您可以将文件转换为
System.Data.DataSet
,其中将包含Excel文件的每个工作表和数据

DataSet dataSet = reader.AsDataSet();

解析Excel文件的一种简单方法是使用库,例如(也可作为提供)。安装后,读取Excel文件非常简单

using Excel;

[HttpPost]
public ActionResult ShowExcelFile(HttpPostedFileBase getFile)
{
    if (getFile != null && getFile.ContentLength > 0)
    {
        // .xlsx
        IExcelDataReader reader = ExcelReaderFactory.CreateOpenXmlReader(getFile.InputStream);

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

        reader.IsFirstRowAsColumnNames = true; // if your first row contains column names
    }

    return PartialView("ShowExcelFile");
}
从这一点上讲,如果不知道Excel文件的内容,就很难说出您的确切需求。您可以将文件转换为
System.Data.DataSet
,其中将包含Excel文件的每个工作表和数据

DataSet dataSet = reader.AsDataSet();

看看。看看。这是可行的,但我如何访问文件值?这是一个Excel文件,我如何读取该文件?如果您需要访问文档本身,您应该获取ado.net适配器并读取该文件。这是可行的,但我如何访问文件值?这是一个Excel文件,所以我如何读取该文件?如果您需要访问文档本身,您应该获取ado.net适配器并读取该文件。我的NuGet中找不到Excel数据读取器,但我尝试手动下载它。我应该把.dll放在哪里?@krillezz。或者您可以通过搜索“ExcelDataReader”在VisualStudio中查找它。只是一个问题,这是否也可以与Ajax.BeginForm一起使用?因为我想在div标签中返回我的partialview通过Ajax上传文件是不可能的,如果我没记错的话,你必须使用一些涉及iframe或Flash的技巧。这可能是一个值得发布的新问题,因为我对它不太熟悉。@Dom完全可以通过ajax上传文件。无法从我的NuGet中找到Excel数据读取器,但我尝试手动下载它。我应该把.dll放在哪里?@krillezz。或者您可以通过搜索“ExcelDataReader”在VisualStudio中查找它。只是一个问题,这是否也可以与Ajax.BeginForm一起使用?因为我想在div标签中返回我的partialview通过Ajax上传文件是不可能的,如果我没记错的话,你必须使用一些涉及iframe或Flash的技巧。这可能是一个值得发布的新问题,因为我对它不太熟悉。@Dom完全可以通过ajax上传文件。