在c#Web应用程序(MVC)中读取csv文件

时间:2018-11-30 15:36:48

标签: c# asp.net-mvc web-applications

我是C#和Visual Studio的新手。我试图将.csv文件读入我的ASP.NET MVC项目中,但找不到正确的教程。我尝试了多种不同的方法,并在下面列出的相同代码段上不断遇到相同的错误。

控制器

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Fifa19TeamBuilder.Models;
using System.IO;
using System.Web;
using System.Web.Mvc;
using Controller = Microsoft.AspNetCore.Mvc.Controller;
using ActionResult = Microsoft.AspNetCore.Mvc.ActionResult;
using Microsoft.AspNetCore.Hosting.Server;

namespace Fifa19TeamBuilder.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult About()
        {
            ViewData["Message"] = "Your application description page.";

            return View();
        }

        public IActionResult Contact()
        {
            ViewData["Message"] = "Your contact page.";

            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [System.Web.Mvc.HttpPost]
        public ActionResult Team(*HttpPostedFileBase* postedFile)
        {
            List<TeamModel> team = new List<TeamModel>();
            string filePath = string.Empty;

            if (postedFile != null)
            {
                string path = *Server*.MapPath("~/Uploads/");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                filePath = path + Path.GetFileName(postedFile.*FileName*);
                string extension = Path.GetExtension(postedFile.*FileName*);
                postedFile.*SaveAs*(filePath);

                //Read the contents of CSV file.
                string csvData = System.IO.File.ReadAllText(filePath);

                //Execute a loop over the rows.
                foreach (string row in csvData.Split('\n'))
                {
                    if (!string.IsNullOrEmpty(row))
                    {
                        team.Add(new TeamModel
                        {
                            ID = Convert.ToInt32(row.Split(',')[0]),
                            player_name = row.Split(',')[1],
                            quality = row.Split(',')[2],
                            overall = row.Split(',')[2],
                            club = row.Split(',')[2],
                            league = row.Split(',')[2],
                            nationality = row.Split(',')[2],
                            position = row.Split(',')[2],
                            age = row.Split(',')[2],
                        });
                    }
                }
            }

            return View(team);
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}

型号

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Fifa19TeamBuilder.Models
{
    public class TeamModel
    {
        public int ID { set; get; }
        public string player_name { set; get; }
        public string quality { set; get; }
        public string overall { set; get; }
        public string club { set; get; }
        public string league { set; get; }
        public string nationality { set; get; }
        public string position { set; get; }
        public string age { set; get; }
    }
}

错误

  

CS0246找不到类型或名称空间名称'HttpPostedFileBase'(您是否缺少using指令或程序集引用?)

错误2:

  

CS0103当前上下文中不存在名称“服务器”

错误3:

  

CS1061'HttpPostedFileBaseModelBinder'不包含'SaveAs'的定义,并且找不到可访问的扩展方法,该方法接受类型为'HttpPostedFileBaseModelBinder'的第一个参数(您是否缺少using指令或引用)

有人可以帮我找到我所有针对VS 2010、2015的教程吗,不确定2017是否对您有所帮助。我只是想从项目文件夹中读取一个csv文件,并将其放在List<>中。我不需要上传文件。我有*表示错误。

这是我获得代码的地方

https://www.aspsnippets.com/Articles/Upload-Read-and-Display-CSV-file-Text-File-data-in-ASPNet-MVC.aspx

0 个答案:

没有答案